Caveats of HttpURLConnection
But while running some performance tests, and trying to figure out a bottleneck issue, we figured out what was wrong in our code.
Join the DZone community and get the full member experience.
Join For FreeDoes this piece of code look OK to you?
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) url.openConnection();
try (InputStream in = url.getInputStream()) {
return streamToString(in);
}
} finally {
if (connection != null) connection.disconnect();
}
Looks good – it opens a connection, reads from it, closes the input stream, releases the connection, and that’s it. But while running some performance tests, and trying to figure out a bottleneck issue, we found out that disconnect()
is not as benign as it seems – when we stopped disconnecting our connections, there were twice as many outgoing connections. Here’s the javadoc:
Indicates that other requests to the server are unlikely in the near future. Calling disconnect() should not imply that this HttpURLConnection instance can be reused for other requests.
And on the class itslef:
Calling the disconnect() method may close the underlying socket if a persistent connection is otherwise idle at that time.
This is still unclear, but gives us a hint that there’s something more. After reading a couple of stackoverflow and java.net answers (1, 2, 3, 4) and also the android documentation of the same class, which is actually different from the Oracle implementation, it turns out that.disconnect()
actually closes (or may close, in the case of android) the underlying socket.
Then we can find this bit of documentation (it is linked in the javadoc, but it’s not immediately obvious that it matters when calling disconnect), which gives us the whole picture:
The keep.alive
property (default: true) indicates that sockets can be reused by subsequent requests. That works by leaving the connection to the server (which supports keep alive) open, and then the overhead of opening a socket is no longer needed. By default, up to 5 such sockets are reused (per destination). You can increase this pool size by setting thehttp.maxConnections
property. However, after increasing that to 10, 20 and 50, there was no visible improvement in the number of outgoing requests.
However, when we switched from HttpURLConnection
to apache http client, with a pooled connection manager, we had 3 times more outgoing connections per second. And that’s without fine-tuning it.
Load testing, i.e. bombarding a target server with as many requests as possible, sounds like a niche use-case. But in fact, if your application invokes a web service, either within your stack, or an external one, as part of each request, then you have the same problem – you will be able to make fewer requests per second to the target server, and consequently, respond to fewer requests per second to your users.
The advice here is: almost always prefer apache http client – it has a way better API and it seems way better performance, without the need to understand how exactly it functions underneath. But be careful of the same caveats there as well – check pool size and connection reuse. If using HttpURLConnection
, do not disconnect your connections after you read their response, consider increasing the socket pool size, and be careful of related problems.
Published at DZone with permission of Bozhidar Bozhanov, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments