blib/lib/LWP/UserAgent/Plugin/Retry.pm

filter: Statementstmt 29.2% 19 / 65 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.1% 24 / 132 CC 25 SCAR 62.9
CC
25
Cov
16%
after_request
650.0
BEGIN
1.0
BEGIN
1.0
CRAP 541.8
↑ Summary
1 package LWP::UserAgent::Plugin::Retry;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Time::HiRes qw(sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
11 1 our $DATE = '2024-07-17'; # DATE
12 1 our $DIST = 'LWP-UserAgent-Plugin-Retry'; # DIST
13 1 our $VERSION = '0.005'; # VERSION
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{LWP_USERAGENT_PLUGIN_RETRY_MAX_ATTEMPTS} // 4;
Condition0%0/4
$r->{"config"}{"max_attempts"} //= $ENV{"LWP_USERAGENT_PLUGIN_RETRY_MAX_ATTEMPTS"} // 4
l!l
00
$ENV{"LWP_USERAGENT_PLUGIN_RETRY_MAX_ATTEMPTS"} // 4
l!l
00
MC/DC0%0/1
$r->{"config"}{"max_attempts"} //= $ENV{"LWP_USERAGENT_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{LWP_USERAGENT_PLUGIN_RETRY_DELAY} // 2;
Condition0%0/4
$r->{"config"}{"delay"} //= $ENV{"LWP_USERAGENT_PLUGIN_RETRY_DELAY"} // 2
l!l
00
$ENV{"LWP_USERAGENT_PLUGIN_RETRY_DELAY"} // 2
l!l
00
MC/DC0%0/1
$r->{"config"}{"delay"} //= $ENV{"LWP_USERAGENT_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->{ua}{_backoff_obj} //=
25 Module::Load::Util::instantiate_class_with_optional_args(
26 0 ▶ {ns_prefix => 'Algorithm::Backoff'}, $r->{config}{strategy});
Condition0%0/3
$r->{"ua"}{"_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->{"ua"}{"_backoff_obj"} //= Module::Load::Util::instantiate_class_with_optional_args({"ns_prefix", "Algorithm::Backoff"}, $r->{"config"}{"strategy"})
$r->{"ua"}{"_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->{"ua"}{"_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 my $code = $r->{response}->code;
31 0 ▶ if (defined $r->{config}{retry_if}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r->{'config'}{'retry_if'}) { } 0 0 0
32 0 my $ref = ref $r->{config}{retry_if};
33 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
34 0 ▶ $is_success++ unless $code =~ $r->{config}{retry_if};
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /$r->{"config"}{"retry_if"}/ 0 0 0
35 } elsif ($ref eq 'ARRAY') {
36 0 ▶ $is_success++ unless grep { $_ == $code } @{ $r->{config}{retry_if} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ == $code;} @{$$r{"config"}{"retry_if"};} 0 0 0
37 } elsif ($ref eq 'CODE') {
38 0 ▶ $is_success++ unless $r->{config}{retry_if}->($class, $r);
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"config"}{"retry_if"}->($class, $r) 0 0 0
39 } else {
40 0 die "Please supply a scalar/Regexp/arrayref/coderef retry_if";
41 }
42 } else {
43 0 ▶ $is_success++ if $code !~ /\A[5]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /\A[5]/ 0 0 0
44 }
45  
46 SUCCESS: {
47 0 ▶ last unless $is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_success 0 0 0
48 0 ▶ if ($r->{ua}{_backoff_obj}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"ua"}{"_backoff_obj"}) 0 0 0
49 0 my $delay_on_success = $r->{ua}{_backoff_obj}->success;
50 0 ▶ if ($delay_on_success > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delay_on_success > 0) 0 0 0
51 0 log_trace "Delaying for %.1f second(s) after successful request", $delay_on_success;
52 0 sleep $delay_on_success;
53 }
54 }
55 0 return -1;
56 }
57  
58 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
59 0 my $max_attempts;
60 my $delay;
61 0 my $should_give_up;
62 0 ▶ if ($r->{ua}{_backoff_obj}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'ua'}{'_backoff_obj'}) { } 0 0 0
63 0 $delay = $r->{ua}{_backoff_obj}->failure;
64 0 ▶ $should_give_up++ if $delay < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $delay < 0 0 0 0
65 0 $max_attempts = $r->{ua}{_backoff_obj}{max_attempts};
66 } else {
67 $should_give_up++ if $r->{config}{max_attempts} &&
68 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
69 0 $max_attempts = $r->{config}{max_attempts};
70 0 $delay = $r->{config}{delay};
71 }
72  
73 0 my ($ua, $request) = @{ $r->{argv} };
74  
75 GIVE_UP: {
76 0 ▶ last unless $should_give_up;
Branch: 1 branch
BranchTrueFalseTotal
unless $should_give_up 0 0 0
77 log_trace "Failed requesting %s %s (%s - %s), giving up",
78 $request->method,
79 $request->uri . "",
80 $r->{response}->code,
81 0 $r->{response}->message;
82 0 return 0;
83 }
84  
85 0 $r->{retries}++;
86  
87 log_trace "Failed requesting %s %s (%s - %s), retrying in %.1f second(s) (attempt %d of %d) ...",
88 $request->method,
89 $request->uri . "",
90 $r->{response}->code,
91 $r->{response}->message,
92 $delay,
93 1+$r->{retries},
94 0 $max_attempts;
95 0 sleep $delay;
96 0 98; # repeat request()
97 }
98  
99 1 1;
100 # ABSTRACT: Retry failed requests
101  
102 __END__
↑ Summary