Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Timeout for connect in Anyevent::HTTP. Help, please.

by 0day (Sexton)
on Sep 16, 2012 at 15:24 UTC ( #993921=perlquestion: print w/ replies, xml ) Need Help??
0day has asked for the wisdom of the Perl Monks concerning the following question:

I can not figure out where and what code I have to insert, to set the time to make a connection.
use AnyEvent::HTTP; http_request GET => "$url", headers => { 'Host' => $host, 'Connection' => 'keep-alive', 'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.8 (KH +TML, like Gecko) Chrome/20.0.1108.0 Safari/536.8', 'Content-Type' => 'application/x-www-form-urlencoded', 'Referer' => $host }, tcp_connect => sub { my ($host, $service, $connect_cb, $prepare_cb) = @_; tcp_connect $ip, $service, sub { $connect_cb->(@_); }, sub { $prepare_cb->(@_); }; }, sub { #my ($body, $hdr) = @_; callback ($_[0], $_[1], $ip, $host, undef, undef, undef, $error_co +unt, $redirect); $cv->end; };

Comment on Timeout for connect in Anyevent::HTTP. Help, please.
Download Code
Re: Timeout for connect in Anyevent::HTTP. Help, please.
by Tanktalus (Canon) on Sep 19, 2012 at 17:48 UTC

    Just a brief look at AnyEvent::HTTP's fine documentation indicates a timeout key. This timeout value is used (and reset) at various stages of the connection, but may suffice for you.

    If not that, then you may need to get a bunch more complex, creating your own timer for on_prepare, keeping the guard object for the overall http_request, and destroying the guard if the timeout happens. I'm not going to provide an example of that only because it's probably more complex than required, so it'd be a waste of time to create it.

      About a simple timeout all clear (Documentation: "timeout => $seconds The time-out to use for various stages - each connect attempt will reset the timeout, as will read or write activity, ie this is not an overall timeout." ) But how to set overall timeout, which does not reset?

        Without testing anything:

        my $w; my $cv = http_request GET => $url, ..., sub { $w = undef; callback(... +) }; # kill the watcher when we're done $w = AE::timer $timeout, 0, sub { $cv = undef }; # kill the http requ +est after $timeout seconds
        However, that may not work depending on what else you're doing - if you're waiting on $cv->recv, you may want to use $cv->send() if $cv inside the timer callback to trigger that.

        Basically, start the request, then start the timer, then wait, and when the http request returns, clear the $cv variable and/or clear $w to terminate the timer. There are a few things to pay attention to here, to clear cv's and watchers at the appropriate time, but it should otherwise be straight forward.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://993921]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2015-07-03 02:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (47 votes), past polls