Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for timeout propagation in async requests #615

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/main/java/io/nats/client/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,20 @@ enum Status {
*/
CompletableFuture<Message> request(Message message);

/**
* Send a request. The returned future will be completed when the
* response comes back.
*
* <p>The Message object allows you to set a replyTo, but in requests,
* the replyTo is reserved for internal use as the address for the
* server to respond to the client with the consumer's reply.</p>
*
* @param message the message
* @param timeout the time to wait for a response
* @return a Future for the response, which may be cancelled on error or timed out
*/
CompletableFuture<Message> requestWithTimeout(Message message, Duration timeout);

/**
* Send a request and returns the reply or null. This version of request is equivalent
* to calling get on the future returned from {@link #request(String, byte[]) request()} with
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/io/nats/client/impl/NatsConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,12 @@ public CompletableFuture<Message> request(String subject, byte[] body) {
return requestFutureInternal(subject, null, body, options.supportUTF8Subjects(), null, true);
}

@Override
public CompletableFuture<Message> requestWithTimeout(Message message, Duration timeout) {
validateNotNull(message, "Message");
return requestFutureInternal(message.getSubject(), message.getHeaders(), message.getData(), message.isUtf8mode(), timeout, true);
}

@Override
public CompletableFuture<Message> request(Message message) {
validateNotNull(message, "Message");
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/io/nats/client/impl/RequestTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,39 @@ public void testRequireCleanupOnTimeoutNoNoResponders() throws IOException, Inte
}
}

@Test
public void testRequireCleanupOnTimeoutCleanCompletable() throws IOException, InterruptedException {
try (NatsTestServer ts = new NatsTestServer(false)) {
Options options = new Options.Builder().server(ts.getURI())
.requestCleanupInterval(Duration.ofHours(1))
.noNoResponders().build();

NatsConnection nc = (NatsConnection) Nats.connect(options);
try {
assertEquals(Connection.Status.CONNECTED, nc.getStatus(), "Connected Status");

NatsMessage nm = NatsMessage.builder().subject(SUBJECT).data(dataBytes(2)).build();
long cleanupInterval = 100;

CompletableFuture<Message> future = nc.requestWithTimeout(nm, Duration.ofMillis(cleanupInterval));

Thread.sleep(2 * cleanupInterval + Options.DEFAULT_CONNECTION_TIMEOUT.toMillis());

nc.cleanResponses(false);

assertTrue(future.isCompletedExceptionally());
assertEquals(0, ((NatsStatistics)nc.getStatistics()).getOutstandingRequests());

} finally {
nc.close();
assertEquals(Connection.Status.CLOSED, nc.getStatus(), "Closed Status");
}
}
}




@Test
public void testRequireCleanupOnCancelFromNoResponders() throws IOException, InterruptedException {
try (NatsTestServer ts = new NatsTestServer(false)) {
Expand Down