blib/lib/LWP/UserAgent/ExponentialBackoff.pm

filter: Statementstmt 90.9% 80 / 88 Branchbran 62.5% 20 / 32 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 64.7% 11 / 17 Podpod 25.0% 4 / 16 totaltotal 75.9% 120 / 158 CC 18 SCAR 16.3
CC
18
Cov
84%
new
10.9
simple_request
5.0
retry
4.0
CRAP 5.1
↑ Summary
1 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package LWP::UserAgent::ExponentialBackoff;
4 2 $VERSION = '0.04';
5 2 @ISA = ("LWP::UserAgent");
6 2 my @FAILCODES = qw(408 500 502 503 504);
7  
8 1 ▶sub new {
Subroutine new: called CC=10 SCAR=23.9
Pod documented
9 1 my ( $class, %cnf ) = @_;
10 1 my $before_request = delete $cnf{before_request};
11 1 my $after_request = delete $cnf{after_request};
12 1 my $sum = delete $cnf{sum};
13 1 my $retryCount = delete $cnf{retryCount};
14 1 ▶ $retryCount = 3 unless defined $retryCount;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $retryCount 1 0 1
15 1 my $minBackoff = delete $cnf{minBackoff};
16 1 ▶ $minBackoff = 3 unless defined $minBackoff;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $minBackoff 0 1 1
17 1 my $maxBackoff = delete $cnf{maxBackoff};
18 1 ▶ $maxBackoff = 90 unless defined $maxBackoff;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $maxBackoff 1 0 1
19 1 my $deltaBackoff = delete $cnf{deltaBackoff};
20 1 ▶ $deltaBackoff = 3 unless defined $deltaBackoff;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $deltaBackoff 1 0 1
21 1 my $tolerance = delete $cnf{tolerance};
22 1 ▶ $tolerance = .20 unless defined $tolerance;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tolerance 1 0 1
23 1 my $failCodes = delete $cnf{failCodes};
24 1 ▶ $failCodes = { map { $_ => $_ } @FAILCODES } unless defined $failCodes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $failCodes 1 0 1
25 1 my $self = $class->SUPER::new(@_);
26 $self = bless {
27 1 %{$self},
28 sum => $sum,
29 retryCount => $retryCount,
30 minBackoff => $minBackoff,
31 maxBackoff => $maxBackoff,
32 tolerance => $tolerance,
33 deltaBackoff => $deltaBackoff,
34 deltaLow => $deltaLow,
35 deltaHigh => $deltaHigh,
36 failCodes => $failCodes
37 }, $class;
38 1 $self->deltas($tolerance);
39 1 ▶ $self->sum($sum) unless !defined $sum;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sum 0 1 1
40 1 ▶ $self->before_request($before_request) unless !defined $before_request;
Branch: 1 branch
BranchTrueFalseTotal
if defined $before_request 1 0 1
41 1 ▶ $self->after_request($after_request) unless !defined $after_request;
Branch: 1 branch
BranchTrueFalseTotal
if defined $after_request 1 0 1
42 1 return $self;
43 }
44  
45 5 ▶sub simple_request {
Subroutine simple_request: called CC=5 SCAR=16.1
Pod documented
46 5 my ( $self, @args ) = @_;
47 5 my $failCodes = $self->{failCodes};
48 5 my $currentRetryCount = 0;
49 5 my $before_c = $self->before_request;
50 5 my $after_c = $self->after_request;
51 5 my $retryInterval = 0;
52
53 do {
54 11 ▶ $before_c and $before_c->( $self, \@args, $retryInterval);
Branch: 1 branch
BranchTrueFalseTotal
$before_c and &$before_c($self, \@args, $retryInterval) 11 0 11
55 11 sleep $retryInterval;
56 11 $response = $self->SUPER::simple_request(@args);
57 11 ▶ $after_c and $after_c->( $self, \@args, $retryInterval, $response);
Branch: 1 branch
BranchTrueFalseTotal
$after_c and &$after_c($self, \@args, $retryInterval, $response) 11 0 11
58 11 $code = $response->code();
59 11 $currentRetryCount++;
60 }while ( ( $retryInterval = $self->retry($currentRetryCount-1) )
61 5 ▶ && ${$failCodes}{$code} );
Branch: 1 branch
BranchTrueFalseTotal
$retryInterval = $self->retry($currentRetryCount - 1) and do { $failCodes }->{$code} and do { do { $before_c and &$before_c($self, \@args, $retryInterval); sleep $retryInterval; $response = $self->SUPER::simple_request(@args); $after_c and &$after_c($self, \@args, $retryInterval, $response); $code = $response->code; ++$currentRetryCount } } 6 5 11
Condition100%3/3
$retryInterval = $self->retry($currentRetryCount - 1) and do { $failCodes }->{$code}
!ll&&!rl&&r
236
MC/DC100%2/2
$retryInterval = $self->retry($currentRetryCount - 1) and do { $failCodes }->{$code}
$retryInterval = $self->retry($currentRetryCount - 1) do { $failCodes }->{$code}
Truth table100%3/3
A and B
A= $retryInterval = $self->retry($currentRetryCount - 1)B= do { $failCodes }->{$code}
ABresult
0X0
100
111
62
63 5 return $response;
64 }
65  
66 11 ▶sub retry {
Subroutine retry: called CC=4 SCAR=13.9
Pod undocumented
67 11 my ( $self, $currentRetryCount ) = @_;
68 11 my $retryCount = $self->{retryCount};
69 11 my $minBackoff = $self->{minBackoff};
70 11 my $maxBackoff = $self->{maxBackoff};
71 11 my $deltaLow = $self->{deltaLow};
72 11 my $deltaHigh = $self->{deltaHigh};
73 11 my $deltaBackoff = $self->{deltaBackoff};
74  
75 11 ▶ if ( $currentRetryCount < $retryCount ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($currentRetryCount < $retryCount) { } 9 2 11
76  
77 #Calculate Exponential backoff with tolerance (deltaLow & deltaHigh)
78 9 my $r = $deltaBackoff;
79 9 ▶ if ( $deltaHigh - $deltaLow != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deltaHigh - $deltaLow != 0) 4 5 9
80 4 $r = rand( $deltaHigh - $deltaLow ) + $deltaLow;
81 }
82 9 $increment = ( 2**$currentRetryCount - 1 ) * $r + $minBackoff;
83 9 ▶ $retryInterval = $increment <= $maxBackoff ? $increment : $maxBackoff;
Branch: 1 branch
BranchTrueFalseTotal
$increment <= $maxBackoff ? : 9 0 9
84 }
85 else {
86 2 $retryInterval = 0;
87 }
88 11 return $retryInterval;
89 }
90  
91 0 ▶sub retryCount { shift->_elem( 'retryCount', @_ ); }
Subroutine retryCount: not called CC=1 SCAR=6.9
Pod undocumented
92 0 ▶sub minBackoff { shift->_elem( 'minBackoff', @_ ); }
Subroutine minBackoff: not called CC=1 SCAR=6.9
Pod undocumented
93 0 ▶sub maxBackoff { shift->_elem( 'maxBackoff', @_ ); }
Subroutine maxBackoff: not called CC=1 SCAR=6.9
Pod undocumented
94 6 ▶sub failCodes { shift->_elem( 'failCodes', @_ ) }
Subroutine failCodes: called CC=1 SCAR=0.0
Pod undocumented
95 6 ▶sub before_request { shift->_elem( 'before_request', @_ ) }
Subroutine before_request: called CC=1 SCAR=0.0
Pod documented
96 6 ▶sub after_request { shift->_elem( 'after_request', @_ ) }
Subroutine after_request: called CC=1 SCAR=0.0
Pod documented
97  
98 1 ▶sub tolerance {
Subroutine tolerance: called CC=1 SCAR=0.0
Pod undocumented
99 1 my ( $self, $tolerance ) = @_;
100 1 $self->{tolerance} = $tolerance;
101 1 $self->deltas($tolerance);
102 }
103  
104 2 ▶sub deltas {
Subroutine deltas: called CC=2 SCAR=6.9
Pod undocumented
105 2 my ( $self, $tolerance ) = @_;
106 2 ▶ if ( $tolerance == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tolerance == 0) { } 1 1 2
107 1 $self->{deltaLow} = 0;
108 1 $self->{deltaHigh} = 0;
109 }
110 else {
111 1 $self->{deltaLow} = $self->{deltaBackoff} * ( 1 - $tolerance );
112 1 $self->{deltaHigh} = $self->{deltaBackoff} * ( 1 + $tolerance );
113 }
114 }
115  
116 0 ▶sub deltaBackoff { shift->_elem( 'deltaBackoff', @_ ); }
Subroutine deltaBackoff: not called CC=1 SCAR=6.9
Pod undocumented
117  
118 0 ▶sub addFailCodes {
Subroutine addFailCodes: not called CC=1 SCAR=6.9
Pod undocumented
119 0 my ( $self, $code ) = @_;
120 0 $self->{failCodes}->{$code} = $code;
121 }
122  
123 0 ▶sub delFailCodes {
Subroutine delFailCodes: not called CC=1 SCAR=6.9
Pod undocumented
124 0 my ( $self, $code ) = @_;
125 0 delete $self->{failCodes}->{$code};
126 }
127  
128 # Given sum and deltaBackoff, compute retryCount and maxBackoff such that total retryIntervals will always be equal to or "slightly" greater than sum.
129 3 ▶sub sum {
Subroutine sum: called CC=1 SCAR=0.0
Pod undocumented
130 3 my ( $self, $sum ) = @_;
131 3 $self->{retryCount} = log2( $sum / $self->{deltaBackoff} + 1 );
132  
133 # maxBackoff should be at least as big as the largest retry interval which is never bigger than the sum, so just make it equal the sum
134 3 $self->{maxBackoff} = $sum;
135 3 $self->{sum} = $sum;
136 }
137  
138 3 ▶sub log2 {
Subroutine log2: called CC=1 SCAR=0.0
Pod undocumented
139 3 my $n = shift;
140 3 return log($n) / log(2);
141 }
142 2 1;
143 __END__
↑ Summary