| 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)}
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
| |||||||||||||||||||||||||||||
| 19 | 97 | ▶ | $sleeper //= sub ($seconds) { Time::HiRes::sleep($seconds) }; | ||||||||||||||||||||||||||
|
Condition66%2/3
$sleeper //= sub ($seconds) {
&Time::HiRes::sleep($seconds);
}
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);
}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 33 | 2 | ▶ | die $error | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($error) and $error->isa("Azure::SDK::Core::Error")
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")
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 48 | 35 | ▶ | last if $attempt >= $max_retries; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 67 | 33 | ▶ | if (my $ms = $response->header('retry-after-ms')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 2 | $ms =~ s/\A\s+|\s+\z//g; | |||||||||||||||||||||||||||
| 69 | 2 | ▶ | if ($ms =~ /\A[0-9]+\z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 2 | my $s = $ms / 1000; | |||||||||||||||||||||||||||
| 71 | 2 | ▶ | return $s > 60 ? 60 : $s; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 31 | ▶ | if (defined(my $ra = $response->header('retry-after'))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 4 | $ra =~ s/\A\s+|\s+\z//g; | |||||||||||||||||||||||||||
| 76 | 4 | ▶ | if ($ra =~ /\A[0-9]+\z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 1 | ▶ | return $ra > 60 ? 60 : $ra + 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 3 | ▶ | if (defined(my $secs = _http_date_delay($ra))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | ▶ | return $secs > 60 ? 60 : $secs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 1 | my $delay = $epoch - time; | |||||||||||||||||||||||||||
| 117 | 1 | ▶ | return $delay > 0 ? $delay : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 17 | 1; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||