LWP::UserAgent::Determined(3pm) | User Contributed Perl Documentation | LWP::UserAgent::Determined(3pm) |
LWP::UserAgent::Determined - a virtual browser that retries errors
use strict; use LWP::UserAgent::Determined; my $browser = LWP::UserAgent::Determined->new; my $response = $browser->get($url, headers... );
This class works just like LWP::UserAgent (and is based on it, by being a subclass of it), except that when you use it to get a web page but run into a possibly-temporary error (like a DNS lookup timeout), it'll wait a few seconds and retry a few times.
It also adds some methods for controlling exactly what errors are considered retry-worthy and how many times to wait and for how many seconds, but normally you needn't bother about these, as the default settings are relatively sane.
This module inherits all of LWP::UserAgent's methods, and adds the following.
If you specify empty-string, this means not to retry at all.
If you specify a string consisting of a single number, like "10", that means that if the first request doesn't succeed, then "$browser->get(...)" (or any other method based on "request" or "simple_request") should wait 10 seconds and try again (and if that fails, then it's final).
If you specify a string with several numbers in it (like "10,30,90"), then that means $browser can retry as that many times (i.e., one initial try, plus a maximum of the three retries, because three numbers there), and that it should wait first those numbers of seconds each time. So "$browser->timing( "10,30,90" )" basically means:
try the request; return it unless it's a temporary-looking error; sleep 10; retry the request; return it unless it's a temporary-looking error; sleep 30; retry the request; return it unless it's a temporary-looking error; sleep 90 the request; return it;
The default value is "1,3,15".
$http_codes_hr = $browser->codes_to_determinate(); delete $http_codes_hr->{408}; $http_codes_hr->{567} = 1;
(You can actually set a whole new hashset with "$browser->codes_to_determinate($new_hr)", but there's usually no benefit to that as opposed to the above.)
The current default is 408 (Timeout) plus some 5xx codes.
The arguments passed to these routines are:
Example use:
$browser->before_determined_callback( sub { print "Trying ", $_[4][0]->uri, " ...\n"; });
This class works by overriding LWP::UserAgent's "simple_request" method with its own around-method that just loops. See the source of this module; it's straightforward. Relatively.
LWP, LWP::UserAgent
Copyright 2004, Sean M. Burke, all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.
Originally created by Sean M. Burke, "sburke@cpan.org"
Currently maintained by Jesse Vincent "jesse@fsck.com"
2021-01-01 | perl v5.32.0 |