line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTTP::Retry; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
33376
|
use 5.008008; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
75
|
|
4
|
2
|
|
|
2
|
|
15
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
96
|
|
5
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
62
|
|
6
|
2
|
|
|
2
|
|
1410
|
use HTTP::Tiny; |
|
2
|
|
|
|
|
84421
|
|
|
2
|
|
|
|
|
577
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
require Exporter; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
11
|
|
|
|
|
|
|
our @EXPORT_OK = qw( http ); |
12
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub http |
15
|
|
|
|
|
|
|
{ |
16
|
3
|
|
|
3
|
0
|
2419
|
my %conf; |
17
|
|
|
|
|
|
|
my $url; |
18
|
0
|
|
|
|
|
0
|
my $timeout; |
19
|
0
|
|
|
|
|
0
|
my $retry; |
20
|
0
|
|
|
|
|
0
|
my $sleep; |
21
|
3
|
100
|
|
|
|
17
|
unless($#_) |
22
|
|
|
|
|
|
|
{ |
23
|
1
|
|
|
|
|
4
|
$url = shift; |
24
|
1
|
|
|
|
|
3
|
$timeout = 5; |
25
|
1
|
|
|
|
|
2
|
$retry = 3; |
26
|
1
|
|
|
|
|
3
|
$sleep = 0; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
else |
29
|
|
|
|
|
|
|
{ |
30
|
2
|
|
|
|
|
11
|
%conf = @_; |
31
|
2
|
|
|
|
|
5
|
$url = $conf{'url'}; |
32
|
2
|
|
100
|
|
|
11
|
$timeout = $conf{'timeout'} || 5; |
33
|
2
|
|
100
|
|
|
10
|
$retry = $conf{'retry'} || 3; |
34
|
2
|
|
100
|
|
|
8
|
$sleep = $conf{'sleep'} || 0; |
35
|
|
|
|
|
|
|
} |
36
|
3
|
50
|
|
|
|
11
|
return "failed cause empty url" unless $url; |
37
|
|
|
|
|
|
|
|
38
|
3
|
|
|
|
|
34
|
my $http_tiny = HTTP::Tiny->new( 'timeout' => $timeout , 'agent' => 'HTTP::Retry'); |
39
|
3
|
|
|
|
|
258
|
my $resp; |
40
|
3
|
|
|
|
|
10
|
for(1 .. $retry) |
41
|
|
|
|
|
|
|
{ |
42
|
3
|
|
|
|
|
113
|
$resp = $http_tiny->get($url); |
43
|
3
|
50
|
|
|
|
139400
|
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__ |