Posted on 2009-03-14 20:24:45-07 by martin1966
connect timeout
Hi, in order to handle requests that take some time to complete (~90sec) it would be nice to be able to specify different timouts for connect (which should be quite fast) and read. This is nice if the host you try to access is hidden behind a firewall so that you can't connect, but you do not get a "connection closed" or "connection refused". The following packages implement this (2 sec) connect timeout. I suggest, that this type of code is added to the LWP code, where the connect_timeout is set to the global timeout unless otherwise specified. Any comments welcome.
Regards
Martin

PS: An analogue example works for LWP::Parallel and is very useful for parallel requests where some of the hosts can't be accessed

package HGF::Protocol::http; use base qw(LWP::Protocol::http); # Implements a connect timeout of 2 sec sub _new_socket { my $self=shift; my $timeout = pop(@_); push(@_,2); my $ret =$self->SUPER::_new_socket(@_); $ret->timeout($timeout); return $ret; }; package HGF::Protocol::http::Socket; use vars qw(@ISA); @ISA = qw(LWP::Protocol::http::Socket); 1;

In main:

use LWP::UserAgent; use LWP::Protocol; use HGF::Protocol::http; LWP::Protocol::implementor('http','HGF::Protocol::http');
Direct Responses: Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.