Perl AnyEvent throwing HTTP Error 595

3.2k views Asked by At

I have been trying to make asynchronous requests using Perl AnyEvent HTTP module with the following code.

my $headers = {
    'Accept'         => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language'=> 'en-us,en;q=0.5',
    'Connection'     => 'keep-alive'
};

$request = http_request (
    GET => "$url",
    timeout => 5, # seconds
    # persistent => 1,
    # keepalive => 1,
    headers => $headers,
    proxy => $proxyRef,
    sub {
        my ($body, $hdr) = @_;
    }
);

I keep getting the following response from the request:

{ 
  'Reason' => 'Connection timed out',
  'URL' => 'url requested',
  'Status' => 595
};

I've been checking the AnyEvent Documentation for reasons for this error, but haven't been successful with that. Cant find other useful threads on this issue except suggestions to retry on timeout, which yields the same result. A simple 'wget' works on the same URL and its alive. Could anyone point out on how to debug this issue?

1

There are 1 answers

0
Remember Monica On

The "Connction timed out" error indicates that the underlying tcp connect timed out. That is, when AnyEvent::HTTP tries to connect to the web server or proxy, the connection request times out (usually after about 30 seconds). There is no way to influence this timeout portably.

Specifically, the problem is not a problem with the request itself (that phase hasn't been entered yet), nor DNS resolution, nor that the server refuses the connection. The problem is that the server doesn't answer, i.e. it is switched off, the internet connection between you and the server failed, or a firewall silently blocks the connection.

Update

I just got a report by a user who ran into this problem due to a slightly different problem - he started an HTTP request and instead of returning into the event loop, he blocked his program for longer than the timeout (using sleep). In other words, he started an HTTP request and then kept the event library from working. When control finally rturned, AnyEvent::HTTP immediatelly timeouted, as it couldn't create the connection in time.

Seeing that your timeout is very small, this could well be the reason - AnyEvent::HTTP cannot know whether the connection timeouts because of a server/network delay or because it simply couldn't get enough CPU time to do the request in the required time.

The fix in such a case is to either icnrease the timeout to be conveniently longer so there is enough time even if little CPU time it available, or to restructure the program to be written in an event-based way, by running the event loop and using a timer instead of sleep.