Skip to content

Commit

Permalink
better request timeout management (#693)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottf authored Jul 28, 2022
1 parent d965855 commit 411c39c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/main/java/io/nats/client/impl/NatsConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,8 @@ CompletableFuture<Message> requestFutureInternal(String subject, Headers headers
boolean oldStyle = options.isOldRequestStyle();
String responseInbox = oldStyle ? createInbox() : createResponseInbox(this.mainInbox);
String responseToken = getResponseToken(responseInbox);
NatsRequestCompletableFuture future = new NatsRequestCompletableFuture(cancelOn503, futureTimeout);
NatsRequestCompletableFuture future =
new NatsRequestCompletableFuture(cancelOn503, futureTimeout == null ? options.getRequestCleanupInterval() : futureTimeout);

if (!oldStyle) {
responsesAwaiting.put(responseToken, future);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.concurrent.CompletableFuture;

public class NatsRequestCompletableFuture extends CompletableFuture<Message> {
private static final long DEFAULT_TIMEOUT = Options.DEFAULT_CONNECTION_TIMEOUT.toMillis(); // currently 2 seconds
private static final long DEFAULT_TIMEOUT = Options.DEFAULT_REQUEST_CLEANUP_INTERVAL.toMillis(); // currently 5 seconds

private final boolean cancelOn503;
private final long timeOutAfter;
Expand All @@ -17,7 +17,8 @@ public class NatsRequestCompletableFuture extends CompletableFuture<Message> {

public NatsRequestCompletableFuture(boolean cancelOn503, Duration timeout) {
this.cancelOn503 = cancelOn503;
timeOutAfter = System.currentTimeMillis() + (timeout == null ? DEFAULT_TIMEOUT : timeout.toMillis());
timeOutAfter = System.currentTimeMillis() + 10 + (timeout == null ? DEFAULT_TIMEOUT : timeout.toMillis());
// 10 extra millis allows for communication time, probably more than needed but...
}

public void cancelClosing() {
Expand Down

0 comments on commit 411c39c

Please sign in to comment.