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.

> 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 

Oleg


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

Reply via email to