Reputation: 11
My Java code uses a shared Apache HttpClient 4.4.1. I set connect timeout, socket timeout and connection request timeout on individual POST request level to 100 milliseconds. It works fine most of the time. However, on high volume of requests (stress test) HttpClient ignores these timeouts; requests can take 20 seconds or more. Here's my code:
private HttpResponse execHttp(HttpRequestBase request, HttpClient client) {
RequestConfig params = RequestConfig.custom()
.setConnectTimeout(100)
.setSocketTimeout(100)
.setConnectionRequestTimeout(100)
.build();
// Set config
request.setConfig(params);
// Measure time
long tStart = System.currentTimeMillis();
// Execute Http request
HttpResponse response = client.execute(request);
// Calculate and print time
long tDiff = System.currentTimeMillis() - tStart;
logger.debug(String.format("HTTPCLIENT EXEC TIME: FROM REQUEST %d / %d / %d MS ACTUAL %d MS",
request.getConfig().getSocketTimeout(),
request.getConfig().getConnectTimeout(),
request.getConfig().getConnectionRequestTimeout(),
tDiff));
return response;
}
In some cases I get log record like this:
2018-10-26 14:18:45,496 [my-thread-1] DEBUG com.mypackage.HttpTimeoutTest - HTTPCLIENT EXEC TIME: FROM REQUEST 100 / 100 / 100 MS ACTUAL 20044 MS
Is there anything missing in the code? Or is there any workaround how to make it always apply the timeout?
Upvotes: 1
Views: 885
Reputation: 3552
Nothing else stands out with your code. If it is a bug with HttpClient, then you could try with the latest version to see if that address it. If it's a bottleneck, then turning on "context logging" of HttpClient might give some insight.
That being said, the socket timeout is only for time between packet arrivals. So if you keep getting packets at a very slow rate, then your total request time can easily be much higher than your timeout periods. If this is happening, then you would need to spawn an asynchronous task to abort the request at a hard timeout interval.
Upvotes: 1