Skip to content

Commit

Permalink
feat: Add shutdown method to client. (#173)
Browse files Browse the repository at this point in the history
Convenience method to shutdown all ManagedChannels in the client.
  • Loading branch information
danielmai authored Dec 24, 2021
1 parent 55a0da6 commit 196a694
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/java/io/dgraph/DgraphAsyncClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import io.dgraph.DgraphProto.Payload;
import io.dgraph.DgraphProto.TxnContext;
import io.dgraph.DgraphProto.Version;
import io.grpc.Channel;
import io.grpc.Context;
import io.grpc.ManagedChannel;
import io.grpc.Metadata;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
Expand Down Expand Up @@ -371,4 +373,20 @@ public AsyncTransaction newReadOnlyTransaction() {
public AsyncTransaction newReadOnlyTransaction(TxnContext context) {
return new AsyncTransaction(this, this.anyClient(), context, true);
}

/** Calls %{@link io.grpc.ManagedChannel#shutdown} on all connections for this client */
public CompletableFuture<Void> shutdown() {
CompletableFuture<Void> future =
CompletableFuture.runAsync(
() -> {
for (DgraphGrpc.DgraphStub stub : this.stubs) {
Channel chan = stub.getChannel();
if (chan instanceof ManagedChannel) {
((ManagedChannel) chan).shutdown();
}
}
},
this.executor);
return future;
}
}
5 changes: 5 additions & 0 deletions src/main/java/io/dgraph/DgraphClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,9 @@ public void login(String userid, String password) {
public void loginIntoNamespace(String userid, String password, long namespace) {
asyncClient.loginIntoNamespace(userid, password, namespace).join();
}

/** Calls %{@link io.grpc.ManagedChannel#shutdown} on all connections for this client */
public void shutdown() {
asyncClient.shutdown().join();
}
}
3 changes: 3 additions & 0 deletions src/test/java/io/dgraph/DgraphIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ public static void beforeClass() throws InterruptedException {

@AfterClass
public static void afterClass() throws InterruptedException {
// Shutdown channel connections
channel1.shutdown().awaitTermination(5, TimeUnit.SECONDS);
channel2.shutdown().awaitTermination(5, TimeUnit.SECONDS);
channel3.shutdown().awaitTermination(5, TimeUnit.SECONDS);
// Or, alternatively, shutdown channels from the client
dgraphClient.shutdown();
}
}

0 comments on commit 196a694

Please sign in to comment.