You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Shawn Heisey <ap...@elyograg.org> on 2016/06/21 14:42:43 UTC
HttpClient is not proving to be thread-safe and reusable -- probably
user error
At the paste URL below is the code I'm using in a test. The test is
checking for race conditions in some server code I've written:
http://apaste.info/Vs6
This code will stop working correctly during the second loop. On the
first loop, it creates 400 threads and requests the URL once in each
thread. On the second loop, it tries to do it again, but hangs after
the 100th thread is created. When I create the client, i set
maxConnPerRoute to 500, so it is related to that. It seems that each
connection isn't being removed from the internal tracking after it
completes.
If I move the httpclient creation inside the for loop and uncomment the
"client.close();" at the end of the loop, then everything works ... but
it seems like Ishould be able to use one client for this entire test.
What have I done wrong in my code?
threads is a Set<Thread>.
firstFailedLoop and loopNum are AtomicInteger.
numThreadsPerRun is set to 400.
If anything in the code is unclear, please let me know what needs
clarification.
Thanks,
Shawn
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org
Re: HttpClient is not proving to be thread-safe and reusable --
probably user error
Posted by Shawn Heisey <ap...@elyograg.org>.
On 6/21/2016 8:56 AM, Pete Keyes wrote:
> You failed to consume the response. Apache-HC will never return the
> connection to the pool. Once you've hit the 500th thread all
> connections are leased. In the run() method simply add a finally to
> your try/catch and move the response object into scope.
Thank you. Added that, and now it all works. I haven't needed to
actually obtain the data in the response, so I never looked that part up
in the documentation. In hindsight, it makes sense that the response
needs handling.
Thanks,
Shawn
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org
Re: HttpClient is not proving to be thread-safe and reusable --
probably user error
Posted by Pete Keyes <PK...@starbucks.com>.
You failed to consume the response. Apache-HC will never return the
connection to the pool. Once you've hit the 500th thread all connections
are leased. In the run() method simply add a finally to your try/catch
and move the response object into scope.
HttpResponse response=null;
try {
response = client.execute(...);
}
catch() {
}
finally {
if(response != null) {
EntityUtils.consumeQuietly(response.getEntity());
}
}
‹
Pete
On 6/21/16, 7:42 AM, "Shawn Heisey" <ap...@elyograg.org> wrote:
>At the paste URL below is the code I'm using in a test. The test is
>checking for race conditions in some server code I've written:
>
>http://apaste.info/Vs6
>
>This code will stop working correctly during the second loop. On the
>first loop, it creates 400 threads and requests the URL once in each
>thread. On the second loop, it tries to do it again, but hangs after
>the 100th thread is created. When I create the client, i set
>maxConnPerRoute to 500, so it is related to that. It seems that each
>connection isn't being removed from the internal tracking after it
>completes.
>
>If I move the httpclient creation inside the for loop and uncomment the
>"client.close();" at the end of the loop, then everything works ... but
>it seems like Ishould be able to use one client for this entire test.
>
>What have I done wrong in my code?
>
>threads is a Set<Thread>.
>firstFailedLoop and loopNum are AtomicInteger.
>numThreadsPerRun is set to 400.
>
>If anything in the code is unclear, please let me know what needs
>clarification.
>
>Thanks,
>Shawn
>
>
>---------------------------------------------------------------------
>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