| 1 | package AnyPAN::RetryPolicy::ExponentialBackoff; | ||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 2 | ▶ | use parent qw/AnyPAN::RetryPolicy/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 2 | ▶ | use Time::HiRes (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 2 | ▶ | use Class::Accessor::Lite ro => [qw/max_retries interval jitter_factor/]; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 0 | ▶ | sub apply { | ||||||||||||
|
Subroutine
apply: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 12 | 0 | my ($self, $code) = @_; | |||||||||||||
| 13 | 0 | ▶ | return sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=8 SCAR=42.8
| |||||||||||||||
| 14 | 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) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 22 | 0 | eval { | |||||||||||||
| 23 | 0 | ▶ | if ($wantarray) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 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 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 32 | # retry | ||||||||||||||
| 33 | 0 | $e = $@; | |||||||||||||
| 34 | 0 | $retry_count++; | |||||||||||||
| 35 | |||||||||||||||
| 36 | 0 | $interval *= 2; | |||||||||||||
| 37 | 0 | ▶ | if ($self->jitter_factor) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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 | ▶ | die $e if $e; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 51 | |||||||||||||||
| 52 | 0 | ▶ | return $wantarray ? @ret : $ret[0]; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 53 | 0 | }; | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | |||||||||||||||
| 56 | 2 | 1; | |||||||||||||
| 57 | __END__ | ||||||||||||||