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 Jean-Marc Spaggiari <je...@spaggiari.org> on 2012/12/17 02:11:48 UTC

Right way to timeout

Hi,

What's the "right" way to configure the timeout with HttpClient 4.2.1?

Here is what I'm doing:

	                HttpParams params = client.getParams();
	                HttpConnectionParams.setConnectionTimeout(params, 30000);
	                HttpConnectionParams.setSoTimeout(params, 30000);
			result.result = client.execute (method);

But I don't think it's working. Sometime it's taking way more than 30 seconds.

How should I proceed?

I have setup a thread to kill the calls when they are taking to much time

Thanks,

JM

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


Re: Right way to timeout

Posted by "Stephen J. Butler" <st...@gmail.com>.
On Mon, Dec 17, 2012 at 7:29 AM, Jean-Marc Spaggiari <
jean-marc@spaggiari.org> wrote:

> I was doing that the oposite way. I mean, I was starting the request
> on a thread, checkinv every 100ms if it was done, and killing it after
> a certain period of time. But some time the abort was not working and
> it was feeling my memory with pending threads...
>

If you're worried about filling your app with abort checking threads then
you could adapt my solution to use only a single thread that runs for the
entire app lifetime. Just (in a multithreaded way) add each request to a
queue sorted by the absolute timeout time. Then your abort thread wakes up
every second and shifts items off the bottom of the queue that should be
aborted.


> If I use such a solution, does it mean that the 2 TimeOut that I'm
> already setting are useless? Will the abort close both the TCP and the
> HTTP connections?
>

I wouldn't say they're useless, but I would suggest setting the thresholds
lower. For example, if the server takes 20 seconds to finish the TCP
connection handshake, will it really perform your request in the remaining
10? Unlikely.

Re: Right way to timeout

Posted by Jean-Marc Spaggiari <je...@spaggiari.org>.
Thanks for sharing.

I was doing that the oposite way. I mean, I was starting the request
on a thread, checkinv every 100ms if it was done, and killing it after
a certain period of time. But some time the abort was not working and
it was feeling my memory with pending threads...

If I use such a solution, does it mean that the 2 TimeOut that I'm
already setting are useless? Will the abort close both the TCP and the
HTTP connections?

Thanks,

JM

2012/12/16, Stephen J. Butler <st...@gmail.com>:
> setConnectionTimeout() only sets a timeout on the initial TCP connection
> request. And setSoTimeout() sets a timeout on the socket operations. But if
> the server is responding to normal TCP packets (whether they have any
> payload or not) then this timeout will never be reached.
>
> If you want to set a timeout on an overall HTTP operation you should setup
> your own thread and call .abort() on the request (this method is
> thread-safe). For example, I use this class and create a new instance of it
> for every request I want to timeout. When I get a response, I interrupt my
> timeout thread.
>
> http://pastebin.com/sRZdZuKx
>
>
> On Sun, Dec 16, 2012 at 7:11 PM, Jean-Marc Spaggiari <
> jean-marc@spaggiari.org> wrote:
>
>> Hi,
>>
>> What's the "right" way to configure the timeout with HttpClient 4.2.1?
>>
>> Here is what I'm doing:
>>
>>                         HttpParams params = client.getParams();
>>                         HttpConnectionParams.setConnectionTimeout(params,
>> 30000);
>>                         HttpConnectionParams.setSoTimeout(params, 30000);
>>                         result.result = client.execute (method);
>>
>> But I don't think it's working. Sometime it's taking way more than 30
>> seconds.
>>
>> How should I proceed?
>>
>> I have setup a thread to kill the calls when they are taking to much time
>>
>> Thanks,
>>
>> JM
>>
>> ---------------------------------------------------------------------
>> 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


Re: Right way to timeout

Posted by "Stephen J. Butler" <st...@gmail.com>.
setConnectionTimeout() only sets a timeout on the initial TCP connection
request. And setSoTimeout() sets a timeout on the socket operations. But if
the server is responding to normal TCP packets (whether they have any
payload or not) then this timeout will never be reached.

If you want to set a timeout on an overall HTTP operation you should setup
your own thread and call .abort() on the request (this method is
thread-safe). For example, I use this class and create a new instance of it
for every request I want to timeout. When I get a response, I interrupt my
timeout thread.

http://pastebin.com/sRZdZuKx


On Sun, Dec 16, 2012 at 7:11 PM, Jean-Marc Spaggiari <
jean-marc@spaggiari.org> wrote:

> Hi,
>
> What's the "right" way to configure the timeout with HttpClient 4.2.1?
>
> Here is what I'm doing:
>
>                         HttpParams params = client.getParams();
>                         HttpConnectionParams.setConnectionTimeout(params,
> 30000);
>                         HttpConnectionParams.setSoTimeout(params, 30000);
>                         result.result = client.execute (method);
>
> But I don't think it's working. Sometime it's taking way more than 30
> seconds.
>
> How should I proceed?
>
> I have setup a thread to kill the calls when they are taking to much time
>
> Thanks,
>
> JM
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>