# Dgraph Java Client: Setting deadlines per call

**URL:** https://discuss.dgraph.io/t/dgraph-java-client-setting-deadlines-per-call/3056
**Category:** Dgraph
**Created:** [August 23, 2018, 3:41am UTC](https://discuss.dgraph.io/t/dgraph-java-client-setting-deadlines-per-call/3056 "2018-08-23T03:41:01Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![deepak](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@deepak](https://discuss.dgraph.io/u/deepak)
#### Post date: [August 23, 2018, 3:41am UTC](https://discuss.dgraph.io/t/dgraph-java-client-setting-deadlines-per-call/3056/1 "2018-08-23T03:41:01Z")

</div>

Hey all, I’m the developer for [dgraph4j](https://github.com/dgraph-io/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](https://grpc.io/blog/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.

```java
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.

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

```

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

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

```

References:

- [https://grpc.io/blog/deadlines](https://grpc.io/blog/deadlines)
- [How does deadline work? · Issue #1495 · grpc/grpc-java · GitHub](https://github.com/grpc/grpc-java/issues/1495)
- [Generate deadline per call from single configuration · Issue #4305 · grpc/grpc-java · GitHub](https://github.com/grpc/grpc-java/issues/4305)

---

<div class="post-metadata">

### Author: ![dmai](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/dmai/32/1254_2.png) [@dmai](https://discuss.dgraph.io/u/dmai)
#### Post date: [September 26, 2018, 9:54pm UTC](https://discuss.dgraph.io/t/dgraph-java-client-setting-deadlines-per-call/3056/2 "2018-09-26T21:54:30Z")

</div>

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

---

<div class="post-metadata">

### Author: ![deepak](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@deepak](https://discuss.dgraph.io/u/deepak)
#### Post date: [September 27, 2018, 5:33pm UTC](https://discuss.dgraph.io/t/dgraph-java-client-setting-deadlines-per-call/3056/3 "2018-09-27T17:33:33Z")

</div>

I had added a section linking to this post earlier: [GitHub - dgraph-io/dgraph4j: Official Dgraph Java client](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.

---

<div class="post-metadata">

### Author: ![dmai](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/dmai/32/1254_2.png) [@dmai](https://discuss.dgraph.io/u/dmai)
#### Post date: [September 27, 2018, 5:38pm UTC](https://discuss.dgraph.io/t/dgraph-java-client-setting-deadlines-per-call/3056/4 "2018-09-27T17:38:45Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![deepak](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@deepak](https://discuss.dgraph.io/u/deepak)
#### Post date: [September 28, 2018, 4:00am UTC](https://discuss.dgraph.io/t/dgraph-java-client-setting-deadlines-per-call/3056/5 "2018-09-28T04:00:11Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![dmai](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/dmai/32/1254_2.png) [@dmai](https://discuss.dgraph.io/u/dmai)
#### Post date: [September 28, 2018, 4:33am UTC](https://discuss.dgraph.io/t/dgraph-java-client-setting-deadlines-per-call/3056/6 "2018-09-28T04:33:53Z")

</div>

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](http://discuss.hypermode.com/t/dgraph-java-client-setting-deadlines-per-call/3056/2), 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.

---

<div class="post-metadata">

### Author: ![deepak](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@deepak](https://discuss.dgraph.io/u/deepak)
#### Post date: [September 28, 2018, 5:28am UTC](https://discuss.dgraph.io/t/dgraph-java-client-setting-deadlines-per-call/3056/7 "2018-09-28T05:28:19Z")

</div>

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.
