Skip to content

Commit

Permalink
Support setting per-request idle timeout, just like the request timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Dong committed Jun 14, 2023
1 parent 28dd546 commit 08215ae
Showing 1 changed file with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

public class JettyClientEngine implements AsyncClientHttpEngine {
public static final String REQUEST_TIMEOUT_MS = JettyClientEngine.class + "$RequestTimeout";
public static final String IDLE_TIMEOUT_MS = JettyClientEngine.class + "$IdleTimeout";
// Yeah, this is the Jersey one, but there's no standard one and it makes more sense to reuse than make our own...
public static final String FOLLOW_REDIRECTS = "jersey.config.client.followRedirects";

Expand Down Expand Up @@ -187,6 +188,19 @@ private void failed(Throwable t) {

private void configureTimeout(final Request request) {
final Object timeout = request.getAttributes().get(REQUEST_TIMEOUT_MS);
final Object idleTimeout = request.getAttributes().get(IDLE_TIMEOUT_MS);
final long timeoutMs = parseTimeoutMs(timeout);
final long idleTimeoutMs = parseTimeoutMs(idleTimeout);
if (timeoutMs > 0) {
request.timeout(timeoutMs, TimeUnit.MILLISECONDS);
}

if (idleTimeoutMs > 0) {
request.idleTimeout(idleTimeoutMs, TimeUnit.MILLISECONDS);
}
}

private long parseTimeoutMs(final Object timeout) {
final long timeoutMs;
if (timeout instanceof Duration) {
timeoutMs = ((Duration) timeout).toMillis();
Expand All @@ -197,9 +211,7 @@ private void configureTimeout(final Request request) {
} else {
timeoutMs = -1;
}
if (timeoutMs > 0) {
request.timeout(timeoutMs, TimeUnit.MILLISECONDS);
}
return timeoutMs;
}

@Override
Expand Down

0 comments on commit 08215ae

Please sign in to comment.