Skip to content

Commit 323251c

Browse files
committed
Apply response timeout to H2 streams
1 parent 9a2a24b commit 323251c

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalH2AsyncExecRuntime.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,17 +297,25 @@ public Cancellable execute(
297297
: exchangeHandler;
298298
final ComplexCancellable complexCancellable = new ComplexCancellable();
299299
final IOSession session = endpoint.session;
300+
final RequestConfig requestConfig = context.getRequestConfigOrDefault();
301+
final Timeout responseTimeout = requestConfig.getResponseTimeout();
300302
if (session.isOpen()) {
301303
if (log.isDebugEnabled()) {
302304
log.debug("{} start execution {}", ConnPoolSupport.getId(endpoint), id);
303305
}
304306
context.setProtocolVersion(HttpVersion.HTTP_2);
305307
session.enqueue(
306-
new RequestExecutionCommand(actual, pushHandlerFactory, complexCancellable, context),
308+
new RequestExecutionCommand(
309+
actual,
310+
pushHandlerFactory,
311+
context,
312+
streamControl -> {
313+
streamControl.setTimeout(responseTimeout);
314+
complexCancellable.setDependency(streamControl);
315+
}),
307316
Command.Priority.NORMAL);
308317
} else {
309318
final HttpRoute route = endpoint.route;
310-
final RequestConfig requestConfig = context.getRequestConfigOrDefault();
311319
@SuppressWarnings("deprecation")
312320
final Timeout connectTimeout = requestConfig.getConnectTimeout();
313321
connPool.getSession(route, connectTimeout, new FutureCallback<IOSession>() {
@@ -321,7 +329,14 @@ public void completed(final IOSession ioSession) {
321329
}
322330
context.setProtocolVersion(HttpVersion.HTTP_2);
323331
ioSession.enqueue(
324-
new RequestExecutionCommand(actual, pushHandlerFactory, complexCancellable, context),
332+
new RequestExecutionCommand(
333+
actual,
334+
pushHandlerFactory,
335+
context,
336+
streamControl -> {
337+
streamControl.setTimeout(responseTimeout);
338+
complexCancellable.setDependency(streamControl);
339+
}),
325340
Command.Priority.NORMAL);
326341
}
327342

httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalH2AsyncClient.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ public Cancellable execute(
142142
}
143143
@SuppressWarnings("deprecation")
144144
final Timeout connectTimeout = requestConfig.getConnectTimeout();
145+
final Timeout responseTimeout = requestConfig.getResponseTimeout();
145146
final HttpHost target = new HttpHost(request.getScheme(), request.getAuthority());
146147

147148
final Future<IOSession> sessionFuture = connPool.getSession(new HttpRoute(target), connectTimeout,
@@ -224,16 +225,22 @@ public void streamEnd(final List<? extends Header> trailers) throws HttpExceptio
224225
new RequestExecutionCommand(
225226
new LoggingAsyncClientExchangeHandler(LOG, exchangeId, internalExchangeHandler),
226227
pushHandlerFactory,
227-
cancellable,
228-
clientContext),
228+
clientContext,
229+
streamControl -> {
230+
streamControl.setTimeout(responseTimeout);
231+
cancellable.setDependency(streamControl);
232+
}),
229233
Command.Priority.NORMAL);
230234
} else {
231235
session.enqueue(
232236
new RequestExecutionCommand(
233237
internalExchangeHandler,
234238
pushHandlerFactory,
235-
cancellable,
236-
clientContext),
239+
clientContext,
240+
streamControl -> {
241+
streamControl.setTimeout(responseTimeout);
242+
cancellable.setDependency(streamControl);
243+
}),
237244
Command.Priority.NORMAL);
238245
}
239246
}

httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/PoolingAsyncClientConnectionManager.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.apache.hc.client5.http.nio.AsyncClientConnectionOperator;
5151
import org.apache.hc.client5.http.nio.AsyncConnectionEndpoint;
5252
import org.apache.hc.client5.http.nio.ManagedAsyncClientConnection;
53+
import org.apache.hc.client5.http.protocol.HttpClientContext;
5354
import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
5455
import org.apache.hc.core5.annotation.Contract;
5556
import org.apache.hc.core5.annotation.Internal;
@@ -819,7 +820,15 @@ public void execute(
819820
}
820821
context.setProtocolVersion(connection.getProtocolVersion());
821822
connection.submitCommand(
822-
new RequestExecutionCommand(exchangeHandler, pushHandlerFactory, context),
823+
new RequestExecutionCommand(
824+
exchangeHandler,
825+
pushHandlerFactory,
826+
context,
827+
streamControl -> {
828+
final HttpClientContext clientContext = HttpClientContext.cast(context);
829+
final Timeout responseTimeout = clientContext.getRequestConfigOrDefault().getResponseTimeout();
830+
streamControl.setTimeout(responseTimeout);
831+
}),
823832
Command.Priority.NORMAL);
824833
}
825834

0 commit comments

Comments
 (0)