line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTTP::Retry; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
41175
|
use 5.008008; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
65
|
|
4
|
2
|
|
|
2
|
|
10
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
60
|
|
5
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
47
|
|
6
|
2
|
|
|
2
|
|
2126
|
use HTTP::Tiny; |
|
2
|
|
|
|
|
128023
|
|
|
2
|
|
|
|
|
544
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
require Exporter; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
11
|
|
|
|
|
|
|
our @EXPORT_OK = qw( http ); |
12
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub http |
15
|
|
|
|
|
|
|
{ |
16
|
3
|
|
|
3
|
0
|
2071
|
my %conf; |
17
|
|
|
|
|
|
|
my $url; |
18
|
0
|
|
|
|
|
0
|
my $timeout; |
19
|
0
|
|
|
|
|
0
|
my $retry; |
20
|
0
|
|
|
|
|
0
|
my $sleep; |
21
|
3
|
100
|
|
|
|
22
|
unless($#_) |
22
|
|
|
|
|
|
|
{ |
23
|
1
|
|
|
|
|
3
|
$url = shift; |
24
|
1
|
|
|
|
|
2
|
$timeout = 5; |
25
|
1
|
|
|
|
|
2
|
$retry = 3; |
26
|
1
|
|
|
|
|
2
|
$sleep = 0; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
else |
29
|
|
|
|
|
|
|
{ |
30
|
2
|
|
|
|
|
12
|
%conf = @_; |
31
|
2
|
|
|
|
|
8
|
$url = $conf{'url'}; |
32
|
2
|
|
100
|
|
|
9
|
$timeout = $conf{'timeout'} || 5; |
33
|
2
|
|
100
|
|
|
9
|
$retry = $conf{'retry'} || 3; |
34
|
2
|
|
100
|
|
|
8
|
$sleep = $conf{'sleep'} || 0; |
35
|
|
|
|
|
|
|
} |
36
|
3
|
50
|
|
|
|
7
|
return "failed cause empty url" unless $url; |
37
|
|
|
|
|
|
|
|
38
|
3
|
|
|
|
|
27
|
my $http_tiny = HTTP::Tiny->new( 'timeout' => $timeout , 'agent' => 'HTTP::Retry'); |
39
|
3
|
|
|
|
|
219
|
my $resp; |
40
|
3
|
|
|
|
|
9
|
for(1 .. $retry) |
41
|
|
|
|
|
|
|
{ |
42
|
3
|
|
|
|
|
85
|
$resp = $http_tiny->get($url); |
43
|
3
|
50
|
|
|
|
237801
|
return $resp if $resp->{success}; |
44
|
0
|
0
|
|
|
|
|
sleep $sleep if $sleep > 0 ; |
45
|
|
|
|
|
|
|
} |
46
|
0
|
|
|
|
|
|
return "failed at timeout=$timeout, retry=$retry, sleep=$sleep"; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
1; |
49
|
|
|
|
|
|
|
__END__ |