| 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
$ENV{"HTTP_TINY_PLUGIN_RETRY_MAX_ATTEMPTS"} // 4
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"}
| |||||||||||||||||||||||||||||||||||||
| 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
$ENV{"HTTP_TINY_PLUGIN_RETRY_DELAY"} // 2
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"}
| |||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | if (defined $r->{config}{strategy}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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"})
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"})
| |||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||
| 29 | 0 | my $is_success; | |||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | if (defined $r->{config}{retry_if}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 31 | 0 | my $ref = ref $r->{config}{retry_if}; | |||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | if ($ref eq 'Regexp' or !$ref) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/3
$ref eq "Regexp" or not $ref
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
| |||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | $is_success++ unless $r->{response}{status} =~ $r->{config}{retry_if}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 34 | } elsif ($ref eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | $is_success++ unless grep { $_ == $r->{response}{status} } @{ $r->{config}{retry_if} }; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 36 | } elsif ($ref eq 'CODE') { | ||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | $is_success++ unless $r->{config}{retry_if}->($class, $r); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||
| 45 | SUCCESS: { | ||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | last unless $is_success; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if ($r->{http}{_backoff_obj}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 48 | 0 | my $delay_on_success = $r->{http}{_backoff_obj}->success; | |||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ($delay_on_success > 0) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$r->{"retries"} //= 0
$r->{"retries"}
Truth table0%0/2
A
A= $r->{"retries"}
| |||||||||||||||||||||||||||||||||||||
| 60 | 0 | my $max_attempts; | |||||||||||||||||||||||||||||||||||
| 61 | my $delay; | ||||||||||||||||||||||||||||||||||||
| 62 | 0 | my $should_give_up; | |||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ($r->{http}{_backoff_obj}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 64 | 0 | $delay = $r->{http}{_backoff_obj}->failure; | |||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | $should_give_up++ if $delay < 0; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$r->{"config"}{"max_attempts"} and 1 + $r->{"retries"} >= $r->{"config"}{"max_attempts"}
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"}
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||