hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rallavagu <rallav...@gmail.com>
Subject Re: Keep-Alive
Date Mon, 13 Jun 2016 18:23:29 GMT


On 6/7/16 6:13 AM, Oleg Kalnichevski wrote:
> On Mon, 2016-06-06 at 13:43 -0700, Rallavagu wrote:
>> HttpClient 4.3.2, JDK 7
>>
>> I was doing something similar to what is talked in this link related to
>> http commons client 3.x
>>
>> http://httpcomponents.10934.n7.nabble.com/quot-Keep-alive-quot-stale-connections-and-socket-reuse-td15315.html
>>
>> I am using PoolingHttpClientConnectionManager with following SocketConfig.
>>
>> SocketConfig socketConfig =
>> SocketConfig.copy(SocketConfig.DEFAULT).setSoKeepAlive(true).setSoReuseAddress(true).build();
>>
>>  From debug logs, I see that connections are maintained in the pool.
>> Here is the snippet from the log.
>>
>> [DEBUG] 06/06/2016 12:58:16
>> org.apache.http.impl.conn.PoolingHttpClientConnectionManager Connection
>> request: [route: {s}->https://<host>:443][total kept alive: 1; route
>> allocated: 1 of 50; total allocated: 1 of 100]
>> [DEBUG] 06/06/2016 12:58:16
>> org.apache.http.impl.conn.PoolingHttpClientConnectionManager Connection
>> leased: [id: 2][route: {s}->https://<host>:443][total kept alive: 0;
>> route allocated: 1 of 50; total allocated: 1 of 100]
>> [DEBUG] 06/06/2016 12:58:16
>> org.apache.http.impl.conn.PoolingHttpClientConnectionManager Connection
>> released: [id: 2][route: {s}->https://<host>:443][total kept alive: 1;
>> route allocated: 1 of 50; total allocated: 1 of 100]
>>
>> What I am noticing is that every time a connection is used (leased) from
>> the pool, a connection is being established (socketConnect) to the
>> server unless requested in quick successions where a connection is
>> detected as "not stale". Snippet from "MainClientExec.java"
>>
>> if (config.isStaleConnectionCheckEnabled()) {
>>              // validate connection
>>              if (managedConn.isOpen()) {
>>                  this.log.debug("Stale connection check");
>>                  if (managedConn.isStale()) {
>>                      this.log.debug("Stale connection detected");
>>                      managedConn.close();
>>                  }
>>              }
>>          }
>>
>>
>> Using AspectJ, I could track connection opening to the server.
>>
>> [INFO] 06/06/2016 12:58:00 com.test.aop.AbstractLogAspect
>> SSLConnectionSocketFactory.java:232.connectSocket arguments [30000,
>> Socket[addr=<host>/<IP Address>,port=443,localport=49439],
>> https://<host>:443, <host>/<IP Address>:443, null,
>> org.apache.http.client.protocol.HttpClientContext@137f7542] Execution
>> time: 313
>>
>> The above call is not happening when "managedConn.isStale()" returns false.
>>
>> As you can see, I would like to avoid the "connectSocket" call as it is
>> expensive. Am I right in my assumption that a "keep-alive" connection is
>> supposed to work on an already established socket so it is being reused?
>
> Yes, you are.

Thanks. I could validate that keep-alive connection works on already 
established socket.
>
>> Also,  I understand that we can't rely on isStale() method. Any
>> comments/suggestions are welcome.
>>
>
> Please consider upgrading to 4.5.2. Stale connection check logic has
> been reworked in 4.5
I have upgraded to 4.5.2 and found that "setValidateAfterInactivity" 
method could be used to check for stale connections.

I have a background thread to detect and clear idle/expired connections 
from PoolingHttpClientConnectionManager. I wonder how would 
"setValidateAfterInactivity" impact that if at all.

I understand that per http/1.1 spec a keep-alive request is assumed to 
be "forever" in case server does not respond with keep-alive header. 
But, connections are in fact not left open forever (in tomcat, it 
defaults to 20 sec; connectionTimeout="20000"). Doesn't it make sense to 
set a response header so a client can manage keep-alive connection more 
gracefully by sending "Connection: close" when they expire? In the case 
of background thread to check expired connections, there won't be any as 
server never responded with keep-alive header and connections are marked 
to be kept alive forever. Is the only solution is to create a servlet 
filter that sets the response header?

Thanks

>
> Oleg
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message