SDK/Core/Policy/Retry.pm

filter: Statementstmt 100% 73 / 73 Branchbran 84.2% 32 / 38 Conditioncond 62.5% 5 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 90.5% 124 / 137 CC 23 SCAR 17.5
CC
23
Cov
92%
_retry_delay
10.1
send
7.0
_http_date_delay
5.0
CRAP 5.7
« SDK/AppConfiguration/ConfigurationLabel.pm ↑ Summary SDK/AppConfiguration/SyncToken.pm »
1 17 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 17 ▶use feature 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 ▶no warnings 'experimental::class';
Subroutine BEGIN: called CC=1 SCAR=0.0
4
5 17 ▶use Azure::SDK::Core::Policy;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 17 class Azure::SDK::Core::Policy::Retry :isa(Azure::SDK::Core::Policy) {
8 17 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶ use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶ use Time::Local ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 17 field $max_retries :param :reader = 3;
13 17 field $base_delay :param :reader = 0.5;
14 17 field $retry_status :param = undef;
15 17 field $sleeper :param = undef;
16
17 97 ▶ ADJUST {
Subroutine __ANON__: called CC=3 SCAR=11.4
18 97 ▶ $retry_status //= { map { $_ => 1 } 408, 429, 500, 502, 503, 504, 599 };
Condition50%1/2
$retry_status //= {map({$_, 1;} 408, 429, 500, 502, 503, 504, 599)}
l!l
097
MC/DC0%0/1
$retry_status //= {map({$_, 1;} 408, 429, 500, 502, 503, 504, 599)}
$retry_status
Truth table50%1/2
A
A= $retry_status
Aresult
11
00
19 97 ▶ $sleeper //= sub ($seconds) { Time::HiRes::sleep($seconds) };
Condition66%2/3
$sleeper //= sub ($seconds) { &Time::HiRes::sleep($seconds); }
l!l&&r!l&&!r
25720
MC/DC0%0/2
$sleeper //= sub ($seconds) { &Time::HiRes::sleep($seconds); }
$sleeper sub ($seconds) { &Time::HiRes::sleep($seconds); }
Truth table66%2/3
A //= B
A= $sleeperB= sub ($seconds) { &Time::HiRes::sleep($seconds); }
ABresult
1X1
011
000
20 }
21
22 137 ▶ method send ($context, $request, $next) {
Subroutine send: called CC=7 SCAR=19.5
Pod documented
23 137 my $attempt = 0;
24 137 my $response;
25
26 137 while (1) {
27 171 $context->begin_attempt;
28
29 171 my $result = eval { $next->($context, $request) };
30 171 my $error = $@;
31
32 171 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 2 169 171
33 2 ▶ die $error
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($error) and $error->isa("Azure::SDK::Core::Error") 1 1 2
Condition66%2/3
builtin::blessed($error) and $error->isa("Azure::SDK::Core::Error")
!ll&&!rl&&r
101
MC/DC50%1/2
builtin::blessed($error) and $error->isa("Azure::SDK::Core::Error")
builtin::blessed($error) $error->isa("Azure::SDK::Core::Error")
Truth table66%2/3
A and B
A= builtin::blessed($error)B= $error->isa("Azure::SDK::Core::Error")
ABresult
0X0
100
111
34 if blessed($error) && $error->isa('Azure::SDK::Core::Error');
35
36 # The transport raised an exception (a connection/network error
37 # thrown instead of returned as a status). Treat it as transient:
38 # retry until attempts are exhausted, then rethrow the last error.
39 1 ▶ die $error if $attempt >= $max_retries;
Branch: 1 branch
BranchTrueFalseTotal
if $attempt >= $max_retries 0 1 1
40 1 $sleeper->( $self->_retry_delay(undef, $attempt) );
41 1 $attempt++;
42 1 next;
43 }
44
45 169 $response = $result;
46
47 169 ▶ last unless $self->_should_retry($response);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_should_retry($response) 134 35 169
48 35 ▶ last if $attempt >= $max_retries;
Branch: 1 branch
BranchTrueFalseTotal
if $attempt >= $max_retries 2 33 35
49
50 33 $sleeper->( $self->_retry_delay($response, $attempt) );
51 33 $attempt++;
52 }
53
54 136 return $response;
55 }
56
57 169 ▶ method _should_retry ($response) {
Subroutine _should_retry: called CC=2 SCAR=6.9
58 169 ▶ return $retry_status->{ $response->status } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$$retry_status{$response->status} ? : 35 134 169
59 }
60
61 # Returns seconds to sleep before the next attempt.
62 # Prefers server-provided Retry-After-Ms / Retry-After headers (capped at 60s)
63 # so Azure throttling responses are honoured; falls back to exponential
64 # backoff with equal jitter. The response is undef on a transport exception.
65 34 ▶ method _retry_delay ($response, $attempt) {
Subroutine _retry_delay: called CC=10 SCAR=23.1
66 34 ▶ if (defined $response) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $response) 33 1 34
67 33 ▶ if (my $ms = $response->header('retry-after-ms')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ms = $response->header("retry-after-ms")) 2 31 33
68 2 $ms =~ s/\A\s+|\s+\z//g;
69 2 ▶ if ($ms =~ /\A[0-9]+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ms =~ /\A[0-9]+\z/u) 2 0 2
70 2 my $s = $ms / 1000;
71 2 ▶ return $s > 60 ? 60 : $s;
Branch: 1 branch
BranchTrueFalseTotal
$s > 60 ? : 1 1 2
72 }
73 }
74 31 ▶ if (defined(my $ra = $response->header('retry-after'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ra = $response->header("retry-after"))) 4 27 31
75 4 $ra =~ s/\A\s+|\s+\z//g;
76 4 ▶ if ($ra =~ /\A[0-9]+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ra =~ /\A[0-9]+\z/u) 1 3 4
77 1 ▶ return $ra > 60 ? 60 : $ra + 0;
Branch: 1 branch
BranchTrueFalseTotal
$ra > 60 ? : 0 1 1
78 }
79 3 ▶ if (defined(my $secs = _http_date_delay($ra))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $secs = _http_date_delay($ra))) 1 2 3
80 1 ▶ return $secs > 60 ? 60 : $secs;
Branch: 1 branch
BranchTrueFalseTotal
$secs > 60 ? : 0 1 1
81 }
82 }
83 }
84
85 # Exponential backoff with equal jitter so a fleet of clients does not
86 # retry in lockstep; the delay falls within [d/2, d] where
87 # d = base_delay * 2 ** attempt.
88 30 my $delay = $base_delay * ( 2 ** $attempt );
89 30 my $half = $delay / 2;
90 30 return $half + rand() * $half;
91 }
92
93 # Parses an RFC 1123 HTTP-date Retry-After value and returns the number of
94 # seconds until that time (never negative), or undef when the value is not an
95 # HTTP-date.
96 3 ▶ sub _http_date_delay ($value) {
Subroutine _http_date_delay: called CC=5 SCAR=16.2
97 3 my %month = (
98 Jan => 0, Feb => 1, Mar => 2, Apr => 3, May => 4, Jun => 5,
99 Jul => 6, Aug => 7, Sep => 8, Oct => 9, Nov => 10, Dec => 11,
100 );
101
102 3 ▶ return undef unless $value =~ m{
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ / \A \s* \w+ ,\s* (\d{1,2}) \s+ (\w{3}) \s+ (\d{4}) \s+ (\d{2}) : (\d{2}) : (\d{2}) \s* GMT \s* \z /ux 1 2 3
103 \A \s* \w+ ,\s*
104 (\d{1,2}) \s+ (\w{3}) \s+ (\d{4}) \s+
105 (\d{2}) : (\d{2}) : (\d{2}) \s* GMT \s* \z
106 }x;
107
108 2 my ($mday, $mon, $year, $hour, $min, $sec) = ($1, $2, $3, $4, $5, $6);
109 2 ▶ return undef unless defined $month{$mon};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $month{$mon} 0 2 2
110
111 2 my $epoch = eval {
112 2 Time::Local::timegm($sec, $min, $hour, $mday, $month{$mon}, $year);
113 };
114 2 ▶ return undef if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 1 2
115
116 1 my $delay = $epoch - time;
117 1 ▶ return $delay > 0 ? $delay : 0;
Branch: 1 branch
BranchTrueFalseTotal
$delay > 0 ? : 1 0 1
118 }
119 }
120
121 17 1;
122
123 __END__
« SDK/AppConfiguration/ConfigurationLabel.pm ↑ Summary SDK/AppConfiguration/SyncToken.pm »