I have created a server that implements a queue on the server-side, where requests are intentionally held for 11 seconds before a response is sent. For testing purposes, I configured a client application to make requests to this server with a 10-second timeout.
Issue:
Despite setting the client-side timeout to 10 seconds, I am still receiving responses for some requests after the 10-second timeout threshold, even though the server is deliberately holding the request for 11 seconds.
PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder.create()
.setTlsStrategy(ClientTlsStrategyBuilder.create()
.setSslContext(SSLContexts.createSystemDefault())
.setTlsVersions(TLS.V_1_3)
.build())
.setPoolConcurrencyPolicy(PoolConcurrencyPolicy.STRICT)
.setConnPoolPolicy(PoolReusePolicy.LIFO)
.setDefaultConnectionConfig(ConnectionConfig.custom()
.setSocketTimeout(Timeout.ofMilliseconds(10000))
.setConnectTimeout(Timeout.ofMilliseconds(10000))
.setTimeToLive(TimeValue.ofMilliseconds(10000))
.build())
.setDefaultTlsConfig(TlsConfig.custom()
.setVersionPolicy(HttpVersionPolicy.NEGOTIATE)
.setHandshakeTimeout(Timeout.ofMinutes(1))
.build())
.build();
connectionManager.setDefaultMaxPerRoute(100);
connectionManager.setMaxTotal(100);
CloseableHttpAsyncClient asyncclient = HttpAsyncClients.custom()
.setConnectionManager(connectionManager)
.setIOReactorConfig(IOReactorConfig.custom()
.setSoTimeout(Timeout.ofMilliseconds(10000))
.build())
.build();
asyncclient.start();
futures.add(asyncclient.execute(httpRequest, new FutureCallback<>() {
@Override
public void completed(SimpleHttpResponse response) {
long processingTime = System.currentTimeMillis() - startTime;
System.out.println("Request "+ requestId+" completed with status: "+response.getCode()+" processingTime: "+pr`enter code here`ocessingTime);
}
@Override
public void failed(Exception ex) {
}
@Override
public void cancelled() {
}
}));
OutPut:
Request 33 completed with status: 200 processingTime: 2181
Request 29 completed with status: 200 processingTime: 2193
Request 551 completed with status: 200 processingTime: 9175
Request 935 completed with status: 200 processingTime: 14286
Request 1006 completed with status: 200 processingTime: 14284
Request 1122 completed with status: 200 processingTime: 16330
Request 1076 completed with status: 200 processingTime: 16332
Request 1084 completed with status: 200 processingTime: 16333
I have created a server that implements a queue on the server-side, where requests are intentionally held for 11 seconds before a response is sent. For testing purposes, I configured a client application to make requests to this server with a 10-second timeout.
Issue:
Despite setting the client-side timeout to 10 seconds, I am still receiving responses for some requests after the 10-second timeout threshold, even though the server is deliberately holding the request for 11 seconds.
PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder.create()
.setTlsStrategy(ClientTlsStrategyBuilder.create()
.setSslContext(SSLContexts.createSystemDefault())
.setTlsVersions(TLS.V_1_3)
.build())
.setPoolConcurrencyPolicy(PoolConcurrencyPolicy.STRICT)
.setConnPoolPolicy(PoolReusePolicy.LIFO)
.setDefaultConnectionConfig(ConnectionConfig.custom()
.setSocketTimeout(Timeout.ofMilliseconds(10000))
.setConnectTimeout(Timeout.ofMilliseconds(10000))
.setTimeToLive(TimeValue.ofMilliseconds(10000))
.build())
.setDefaultTlsConfig(TlsConfig.custom()
.setVersionPolicy(HttpVersionPolicy.NEGOTIATE)
.setHandshakeTimeout(Timeout.ofMinutes(1))
.build())
.build();
connectionManager.setDefaultMaxPerRoute(100);
connectionManager.setMaxTotal(100);
CloseableHttpAsyncClient asyncclient = HttpAsyncClients.custom()
.setConnectionManager(connectionManager)
.setIOReactorConfig(IOReactorConfig.custom()
.setSoTimeout(Timeout.ofMilliseconds(10000))
.build())
.build();
asyncclient.start();
futures.add(asyncclient.execute(httpRequest, new FutureCallback<>() {
@Override
public void completed(SimpleHttpResponse response) {
long processingTime = System.currentTimeMillis() - startTime;
System.out.println("Request "+ requestId+" completed with status: "+response.getCode()+" processingTime: "+pr`enter code here`ocessingTime);
}
@Override
public void failed(Exception ex) {
}
@Override
public void cancelled() {
}
}));
OutPut:
Request 33 completed with status: 200 processingTime: 2181
Request 29 completed with status: 200 processingTime: 2193
Request 551 completed with status: 200 processingTime: 9175
Request 935 completed with status: 200 processingTime: 14286
Request 1006 completed with status: 200 processingTime: 14284
Request 1122 completed with status: 200 processingTime: 16330
Request 1076 completed with status: 200 processingTime: 16332
Request 1084 completed with status: 200 processingTime: 16333
Share
Improve this question
edited 10 hours ago
ok2c
27.6k5 gold badges65 silver badges73 bronze badges
asked 2 days ago
HiteshHitesh
2812 gold badges5 silver badges20 bronze badges
1 Answer
Reset to default 0- The socket timeout is NOT a deadline. It represents a maximum period of inactivity between consecutive i/o events. If one sets to the socket timeout to 5 seconds and the opposite endpoints sends a packet every 4 seconds, the message exchange will never time out.
- The async (NIO based) message transport used by async HttpClient has a timeout precision of one seconds by default. In some cases, for instance under load, the timeout precision may be off by several seconds. One can increase the precision of timeout events at the cost of greater CPU utilization.