JerseyClient does not respect keepAlive parameter
JerseyClient does not respect keepAlive parameter - underlying HttpClient uses DefaultConnectionKeepAliveStrategy. If the server does not specify keep-alive timeout in the response headers the connection will survive in the connection pool until it's closed.
In case of idle connections they tend to be closed on the server side (which connection pool is unaware of). The result is that the next request:
- leases a (stale) connection from the pool
- sends the request
- tried to read from the underlying socket but fails
- invalidates the connection and retries AFAIR there are 2 retry attempts (retry configuration is also ignored).
In addition it's not possible to easily fix it because the client is built inside the constructor of org.glassfish.jersey.apache.connector.ApacheConnector (which is package-protected). It's not possible to change ConnectionKeepAliveStrategy via reflection because the field in question is final.
I see following options: 1 - spawn a separate thread which will periodically expunge idle connections - see http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d5e405 2 - write own version of ApacheConnector which builds underlying HttpClient correctly (could reuse HttpClientBuilder here). It would mean reverting pull request #791 and doing it differently, but it would fix all the things that were broken in JerseyClient in 8.0
I think the 2nd seems to be the best as it would allow to have the same code/bahavior of HttpClient in both cases (when using jersey or apache clients)
+1, I've created an issue in Jersey's JIRA about keepAlive a month ago https://java.net/jira/browse/JERSEY-2736
+1
I really don't understand how Dropwizard team is discussing about actually releasing a 0.8 version without supporting the ability to properly configure Jersey Client's keepalive and retry policies.
@pparth That's simple: Until now nobody seemed to require it really (there wasn't any bug reported on this before) and it's something we can easily fix in a patch release (i. e. Dropwizard 0.8.1).
Comparing to Dropwizard 0.6.X, Jersey Client support in 0.8.X is actually crippled. Most of the configuration options are not even working. I have the feeling that we are not talking here about "bugs" but large sections of missing functionality that was previously supported and it somehow got missed in the latest versions. Nevertheless, we will try to add bugs for each and every one of them if you feel that this is the way we should handle them.
I've recently started to work on a custom version of ApacheConnector for the Jersey client.
It uses a pre-configured Apache's CloseableHttpClient by HttpClientBuilder rather than creates own from parameters on the fly.
It's more concise and reasonably designed than the default connector as I believe. And, more importantly, it correctly behaves according to the configuration.
If I am not mistaken and I didn't make any blunders during implementation, it should close all open problems with the Jersey Client.
It's not finished yet and there is a lot a work to do (testing, documentation, review), but I think it's the right approach with integration of Apache and Jersey clients.
Also, unfortunately, if we use this approach it means we should revert the approach taken in #791 with a base class that contains common configuration.
@arteam cool, are you going to propose it for official usage in Dropwizard, when it would be done? Btw there is another issue new ApacheConnector could fix: #863