AnyPAN/RetryPolicy/ExponentialBackoff.pm

filter: Statementstmt 37.2% 16 / 43 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 71.4% 5 / 7 Podpod 0.0% 0 / 1 totaltotal 32.3% 21 / 65 CC 8 SCAR 37.3
CC
8
Cov
28%
__ANON__
72.0
apply
2.0
BEGIN
1.0
CRAP 41.6
↑ Summary AnyPAN/CLI.pm »
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
BranchTrueFalseTotal
if ($retry_count < $self->max_retries) 0 0 0
22 0 eval {
23 0 ▶ if ($wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wantarray) { } 0 0 0
elsif (defined $wantarray) { } 0 0 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 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
32 # retry
33 0 $e = $@;
34 0 $retry_count++;
35  
36 0 $interval *= 2;
37 0 ▶ if ($self->jitter_factor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->jitter_factor) 0 0 0
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
BranchTrueFalseTotal
if $e 0 0 0
51  
52 0 ▶ return $wantarray ? @ret : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
53 0 };
54 }
55  
56 2 1;
57 __END__
↑ Summary AnyPAN/CLI.pm »