Dgraph Java Client: Setting deadlines per call

Hey all, I’m the developer for dgraph4j, the Java client for Dgraph. Here’s something to keep in mind when using the client library.

Dgraph clients communicate over GRPC with the Dgraph server. By default, there is no deadline set on the calls made by the client. GRPC deadlines work a bit differently from regular client deadlines, because they also control the timeout value on the server side. So, if the GRPC-specific deadline is not set by the client, the Dgraph server will continue to process the request, even if the client has timed out. This could lead to a wastage of resources.

The GRPC deadlines are set in the Java client using the withDeadlineAfter method on the DgraphStub object.

channel = ManagedChannelBuilder.forAddress("localhost", 9080).usePlaintext(true).build();
DgraphGrpc.DgraphStub stub = DgraphGrpc.newStub(channel);
stub.withDeadlineAfter(500, TimeUnit.MILLISECONDS);
DgraphClient dgraphClient = new DgraphClient(stub);

However, there is a problem with the code above. It sets the deadline only once, starting at the point the code above is run. It cannot be used for making multiple calls, each one having a deadline of 500ms before timing out. To fix that, one needs to initialize a ClientInterceptor that will run on every call and set a fresh deadline.

    ClientInterceptor timeoutInterceptor = new ClientInterceptor(){
      public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
          MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
        return next.newCall(method, callOptions.withDeadlineAfter(500, TimeUnit.MILLISECONDS));

This functionality could also be extracted out into a reusable class called TimeoutInterceptor for example, and then used:

stub.withInterceptor(new TimeoutInterceptor(100, TimeUnit.MILLISECONDS));



@deepak Can you add this info to the dgraph4j README docs?

I had added a section linking to this post earlier: https://github.com/dgraph-io/dgraph4j#setting-deadlines

Just added an entry to the README TOC as well, which should hopefully make it easier to find.

Thanks for adding the TOC entry! I didn’t see it earlier, so that definitely helps.

Can you integrate the forum content into the docs instead of linking to the post? That’d make it even more discoverable.

I was initially reluctant to add the forum content into the README because I wanted to keep things in one place, and avoid repeating the same thing. Moreover, almost the entire entry pertains to grpc rather than dgraph4j, so adding it to the dgraph4j README seemed like a bit of a distraction.

Right now, if somebody is skimming through the README or searching for deadlines related stuff specifically, they have a point to jump off to this forum post and read it fully.

If you still think it is better for the text to be in the body of the README, I will make that change.

If it’s something developers should know when using the client, then it should be in the documentation.

We’ve had at least one instance where someone reached out saying it wasn’t obvious how to set timeouts and that it should be in the docs. This was right when I asked you yesterday, and, lo and behold, it was already in the docs! A pleasant surprise, yet I and others still didn’t see it at the time.

I think it’s at least worth adding a code snippet example, if not elaborating more in the docs.

Adding a code snippet to the README makes sense. I did that, and also retained the link to this forum post. For more context, people can follow the link and read more in detail.

1 Like