Is it wise to keep the HttpUrlConnection open indefinitely until the remote REST endpoint?
I want to optimize a process that runs constantly and makes frequent calls (> 1 per second) to an external API via a simple REST style HTTP post. One thing I noticed is that currently the HttpUrlConnection is created and closed for every API call according to the following structure (unnecessary code and error handling removed for readability).
//every API call
try {
URL url = new URL("..remote_site..");
conn = (HttpURLConnection) url.openConnection();
setupConnectionOptions(conn); //sets things like timeoout and usecaches false
outputWriter = new OutputStreamWriter(new BufferedOutputStream(conn.getOutputStream()));
//send request
} finally {
conn.disconnect();
outputWriter.close();
}
I don't have much experience with the http protocol directly, but based on common sense / knowledge of sockets in general it seems that it would be much more efficient to only create the connection once and reuse it, and only re-initialize it on issue to avoid negotiating the connection every time, for example:
//on startup, or error
private void initializeConnection()
{
URL url = new URL("..remote_site..");
conn = (HttpURLConnection) url.openConnection();
setupConnectionOptions(conn); //sets things like timeoout and usecaches false
}
//per request
try {
outputWriter = new OutputStreamWriter(new BufferedOutputStream(conn.getOutputStream()));
//send request
} catch (IOException) {
try conn.disconnect();
initializeConnection();
} finally {
outputWriter.close();
}
//on graceful exit
conn.disconnect();
My questions:
- is this a reasonable optimization in general (will the speed increase be noticeable)?
Assuming yes:
- should the output stream and connection be reused?
- Is it wise to re-initialize the connection on error, or should I do this after a certain number of requests / time?
source to share
Basically yes, and it saves a lot of time - setting up a socket takes a lot of effort, which is even worse with SSL. This is why "keepalive" has been around since the old days. This is a litle bit contrary to the REST philosophy, but it is a performance optimization.
The only thing related to the fact that sockets are a limited resource; in a really tough production environment, you might lose sockets for new connections. this is a bad thing.
source to share