blib/lib/HTTP/Tiny/Plugin/Retry.pm

filter: Statementstmt 29.6% 19 / 64 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 83.3% 5 / 6 Podpod 0.0% 0 / 1 totaltotal 18.3% 24 / 131 CC 25 SCAR 62.9
CC
25
Cov
17%
after_request
650.0
BEGIN
1.0
BEGIN
1.0
CRAP 541.8
↑ Summary
1 package HTTP::Tiny::Plugin::Retry;
2  
3 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4 1 our $DATE = '2020-08-14'; # DATE
5 1 our $DIST = 'HTTP-Tiny-Plugin-Retry'; # DIST
6 1 our $VERSION = '0.004'; # VERSION
7  
8 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Time::HiRes qw(sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶sub after_request {
Subroutine after_request: not called CC=25 SCAR=64.8
Pod undocumented
16 0 my ($class, $r) = @_;
17  
18 $r->{config}{max_attempts} //=
19 0 ▶ $ENV{HTTP_TINY_PLUGIN_RETRY_MAX_ATTEMPTS} // 4;
Condition0%0/4
$r->{"config"}{"max_attempts"} //= $ENV{"HTTP_TINY_PLUGIN_RETRY_MAX_ATTEMPTS"} // 4
l!l
00
$ENV{"HTTP_TINY_PLUGIN_RETRY_MAX_ATTEMPTS"} // 4
l!l
00
MC/DC0%0/1
$r->{"config"}{"max_attempts"} //= $ENV{"HTTP_TINY_PLUGIN_RETRY_MAX_ATTEMPTS"} // 4
$r->{"config"}{"max_attempts"}
Truth table0%0/2
A
A= $r->{"config"}{"max_attempts"}
Aresult
11
00
20 $r->{config}{delay} //=
21 0 ▶ $ENV{HTTP_TINY_PLUGIN_RETRY_DELAY} // 2;
Condition0%0/4
$r->{"config"}{"delay"} //= $ENV{"HTTP_TINY_PLUGIN_RETRY_DELAY"} // 2
l!l
00
$ENV{"HTTP_TINY_PLUGIN_RETRY_DELAY"} // 2
l!l
00
MC/DC0%0/1
$r->{"config"}{"delay"} //= $ENV{"HTTP_TINY_PLUGIN_RETRY_DELAY"} // 2
$r->{"config"}{"delay"}
Truth table0%0/2
A
A= $r->{"config"}{"delay"}
Aresult
11
00
22 0 ▶ if (defined $r->{config}{strategy}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r->{"config"}{"strategy"}) 0 0 0
23 0 require Module::Load::Util;
24 $r->{http}{_backoff_obj} //=
25 Module::Load::Util::instantiate_class_with_optional_args(
26 0 ▶ {ns_prefix => 'Algorithm::Backoff'}, $r->{config}{strategy});
Condition0%0/3
$r->{"http"}{"_backoff_obj"} //= Module::Load::Util::instantiate_class_with_optional_args({"ns_prefix", "Algorithm::Backoff"}, $r->{"config"}{"strategy"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$r->{"http"}{"_backoff_obj"} //= Module::Load::Util::instantiate_class_with_optional_args({"ns_prefix", "Algorithm::Backoff"}, $r->{"config"}{"strategy"})
$r->{"http"}{"_backoff_obj"} Module::Load::Util::instantiate_class_with_optional_args({"ns_prefix", "Algorithm::Backoff"}, $r->{"config"}{"strategy"})
Truth table0%0/3
A //= B
A= $r->{"http"}{"_backoff_obj"}B= Module::Load::Util::instantiate_class_with_optional_args({"ns_prefix", "Algorithm::Backoff"}, $r->{"config"}{"strategy"})
ABresult
1X1
011
000
27 }
28  
29 0 my $is_success;
30 0 ▶ if (defined $r->{config}{retry_if}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r->{'config'}{'retry_if'}) { } 0 0 0
31 0 my $ref = ref $r->{config}{retry_if};
32 0 ▶ if ($ref eq 'Regexp' or !$ref) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'Regexp' or not $ref) { } 0 0 0
elsif ($ref eq 'ARRAY') { } 0 0 0
elsif ($ref eq 'CODE') { } 0 0 0
Condition0%0/3
$ref eq "Regexp" or not $ref
l!l&&r!l&&!r
000
MC/DC0%0/2
$ref eq "Regexp" or not $ref
$ref eq "Regexp" not $ref
Truth table0%0/3
A or not(B)
A= $ref eq "Regexp"B= not $ref
ABresult
1X1
011
000
33 0 ▶ $is_success++ unless $r->{response}{status} =~ $r->{config}{retry_if};
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"response"}{"status"} =~ /$r->{"config"}{"retry_if"}/ 0 0 0
34 } elsif ($ref eq 'ARRAY') {
35 0 ▶ $is_success++ unless grep { $_ == $r->{response}{status} } @{ $r->{config}{retry_if} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ == $r->{"response"}{"status"};} @{$$r{"config"}{"retry_if"};} 0 0 0
36 } elsif ($ref eq 'CODE') {
37 0 ▶ $is_success++ unless $r->{config}{retry_if}->($class, $r);
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"config"}{"retry_if"}->($class, $r) 0 0 0
38 } else {
39 0 die "Please supply a scalar/Regexp/arrayref/coderef retry_if";
40 }
41 } else {
42 0 ▶ $is_success++ if $r->{response}{status} !~ /\A[5]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"response"}{"status"} =~ /\A[5]/ 0 0 0
43 }
44  
45 SUCCESS: {
46 0 ▶ last unless $is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_success 0 0 0
47 0 ▶ if ($r->{http}{_backoff_obj}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"http"}{"_backoff_obj"}) 0 0 0
48 0 my $delay_on_success = $r->{http}{_backoff_obj}->success;
49 0 ▶ if ($delay_on_success > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delay_on_success > 0) 0 0 0
50 0 log_trace "Delaying for %.1f second(s) after successful request", $delay_on_success;
51 0 sleep $delay_on_success;
52 }
53 }
54 0 return -1;
55 }
56  
57 # FAILURE
58  
59 0 ▶ $r->{retries} //= 0;
Condition0%0/2
$r->{"retries"} //= 0
l!l
00
MC/DC0%0/1
$r->{"retries"} //= 0
$r->{"retries"}
Truth table0%0/2
A
A= $r->{"retries"}
Aresult
11
00
60 0 my $max_attempts;
61 my $delay;
62 0 my $should_give_up;
63 0 ▶ if ($r->{http}{_backoff_obj}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'http'}{'_backoff_obj'}) { } 0 0 0
64 0 $delay = $r->{http}{_backoff_obj}->failure;
65 0 ▶ $should_give_up++ if $delay < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $delay < 0 0 0 0
66 0 $max_attempts = $r->{http}{_backoff_obj}{max_attempts};
67 } else {
68 $should_give_up++ if $r->{config}{max_attempts} &&
69 0 ▶ 1+$r->{retries} >= $r->{config}{max_attempts};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"config"}{"max_attempts"} and 1 + $r->{"retries"} >= $r->{"config"}{"max_attempts"} 0 0 0
Condition0%0/3
$r->{"config"}{"max_attempts"} and 1 + $r->{"retries"} >= $r->{"config"}{"max_attempts"}
!ll&&!rl&&r
000
MC/DC0%0/2
$r->{"config"}{"max_attempts"} and 1 + $r->{"retries"} >= $r->{"config"}{"max_attempts"}
$r->{"config"}{"max_attempts"} 1 + $r->{"retries"} >= $r->{"config"}{"max_attempts"}
Truth table0%0/3
A and B
A= $r->{"config"}{"max_attempts"}B= 1 + $r->{"retries"} >= $r->{"config"}{"max_attempts"}
ABresult
0X0
100
111
70 0 $max_attempts = $r->{config}{max_attempts};
71 0 $delay = $r->{config}{delay};
72 }
73  
74 0 my ($http, $method, $url, $options) = @{ $r->{argv} };
75  
76 GIVE_UP: {
77 0 ▶ last unless $should_give_up;
Branch: 1 branch
BranchTrueFalseTotal
unless $should_give_up 0 0 0
78 log_trace "Failed requesting %s %s (%s - %s), giving up",
79 $method,
80 $url,
81 $r->{response}{status},
82 0 $r->{response}{reason};
83 0 return 0;
84 }
85  
86 0 $r->{retries}++;
87  
88 log_trace "Failed requesting %s %s (%s - %s), retrying in %.1f second(s) (attempt %d of %d) ...",
89 $method,
90 $url,
91 $r->{response}{status},
92 $r->{response}{reason},
93 $delay,
94 1+$r->{retries},
95 0 $max_attempts;
96 0 sleep $delay;
97 0 98; # repeat request()
98 }
99  
100 1 1;
101 # ABSTRACT: Retry failed request
102  
103 __END__
↑ Summary