line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package AnyPAN::RetryPolicy::ExponentialBackoff; |
2
|
2
|
|
|
2
|
|
14
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
68
|
|
3
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
58
|
|
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
10
|
use parent qw/AnyPAN::RetryPolicy/; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
14
|
|
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
85
|
use Time::HiRes (); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
44
|
|
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
10
|
use Class::Accessor::Lite ro => [qw/max_retries interval jitter_factor/]; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
10
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub apply { |
12
|
0
|
|
|
0
|
0
|
|
my ($self, $code) = @_; |
13
|
|
|
|
|
|
|
return sub { |
14
|
0
|
|
|
0
|
|
|
my $wantarray = wantarray; |
15
|
|
|
|
|
|
|
|
16
|
0
|
|
|
|
|
|
my $e; |
17
|
0
|
|
|
|
|
|
my $retry_count = 0; |
18
|
0
|
|
|
|
|
|
my $interval = $self->interval * 1_000_000; # usec |
19
|
|
|
|
|
|
|
|
20
|
0
|
|
|
|
|
|
my @ret; |
21
|
0
|
|
|
|
|
|
while ($retry_count < $self->max_retries) { |
22
|
0
|
|
|
|
|
|
eval { |
23
|
0
|
0
|
|
|
|
|
if ($wantarray) { |
|
|
0
|
|
|
|
|
|
24
|
0
|
|
|
|
|
|
@ret = $code->($retry_count, $e); |
25
|
|
|
|
|
|
|
} elsif (defined $wantarray,) { |
26
|
0
|
|
|
|
|
|
$ret[0] = $code->($retry_count, $e); |
27
|
|
|
|
|
|
|
} else { |
28
|
0
|
|
|
|
|
|
$code->($retry_count, $e); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
}; |
31
|
0
|
0
|
|
|
|
|
if ($@) { |
32
|
|
|
|
|
|
|
# retry |
33
|
0
|
|
|
|
|
|
$e = $@; |
34
|
0
|
|
|
|
|
|
$retry_count++; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
$interval *= 2; |
37
|
0
|
0
|
|
|
|
|
if ($self->jitter_factor) { |
38
|
0
|
|
|
|
|
|
my $delta = $interval * $self->jitter_factor; |
39
|
0
|
|
|
|
|
|
my $min = $interval - $delta; |
40
|
0
|
|
|
|
|
|
$interval = $min + rand(1+2*$delta); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
Time::HiRes::usleep($interval); |
44
|
0
|
|
|
|
|
|
next; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
$e = undef; |
48
|
0
|
|
|
|
|
|
last; |
49
|
|
|
|
|
|
|
} |
50
|
0
|
0
|
|
|
|
|
die $e if $e; |
51
|
|
|
|
|
|
|
|
52
|
0
|
0
|
|
|
|
|
return $wantarray ? @ret : $ret[0]; |
53
|
0
|
|
|
|
|
|
}; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
1; |
57
|
|
|
|
|
|
|
__END__ |