blib/lib/HTTP/Tiny/CustomRetry.pm

filter: Statementstmt 33.9% 20 / 59 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 75.0% 6 / 8 Podpod 100% 2 / 2 totaltotal 30.4% 28 / 92 CC 15 SCAR 44.9
CC
15
Cov
25%
request
156.0
new
20.0
BEGIN
1.0
CRAP 89.0
↑ Summary
1 package HTTP::Tiny::CustomRetry;
2  
3 1 our $DATE = '2019-04-10'; # DATE
4 1 our $VERSION = '0.002'; # VERSION
5  
6 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Time::HiRes qw(sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use parent 'HTTP::Tiny';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
16 0 my ($class, %attrs) = @_;
17  
18 0 my %our_attrs;
19 0 for (keys %attrs) {
20 0 ▶ next unless /^retry_/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^retry_/ 0 0 0
21 0 $our_attrs{$_} = delete $attrs{$_};
22 }
23  
24 0 my $self = $class->SUPER::new(%attrs);
25 0 $self->{$_} = $our_attrs{$_} for keys %our_attrs;
26 0 $self;
27 }
28  
29 0 ▶sub request {
Subroutine request: not called CC=12 SCAR=50.5
Pod documented
30 0 my ($self, $method, $url, $options) = @_;
31  
32 # initiate backoff strategy
33 0 ▶ unless ($self->{_backoff}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_backoff"}) 0 0 0
34  
35 my $strategy = $self->{retry_strategy} //
36 0 ▶ $ENV{HTTP_TINY_CUSTOMRETRY_STRATEGY};
Condition0%0/3
$self->{"retry_strategy"} // $ENV{"HTTP_TINY_CUSTOMRETRY_STRATEGY"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"retry_strategy"} // $ENV{"HTTP_TINY_CUSTOMRETRY_STRATEGY"}
$self->{"retry_strategy"} $ENV{"HTTP_TINY_CUSTOMRETRY_STRATEGY"}
Truth table0%0/3
A // B
A= $self->{"retry_strategy"}B= $ENV{"HTTP_TINY_CUSTOMRETRY_STRATEGY"}
ABresult
1X1
011
000
37 0 ▶ die "Please specify retry_strategy" unless $strategy;
Branch: 1 branch
BranchTrueFalseTotal
unless $strategy 0 0 0
38 0 my $pkg = "Algorithm::Backoff::$strategy";
39 0 (my $pkg_pm = "$pkg.pm") =~ s!::!/!g;
40 0 require $pkg_pm;
41  
42 0 my %ar_attrs;
43 0 for (keys %ENV) {
44 0 ▶ next unless /^HTTP_TINY_CUSTOMRETRY_(.+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^HTTP_TINY_CUSTOMRETRY_(.+)/ 0 0 0
45 0 ▶ next if $1 eq 'strategy';
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "strategy" 0 0 0
46 0 my $name = lc $1;
47 0 $ar_attrs{$name} = $ENV{$_};
48 }
49 0 for (keys %$self) {
50 0 ▶ if (/^retry_(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^retry_(.+)/) 0 0 0
51 0 ▶ next if $1 eq 'strategy';
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "strategy" 0 0 0
52 0 $ar_attrs{$1} = $self->{$_};
53 }
54 }
55  
56 0 $self->{_backoff} = $pkg->new(%ar_attrs);
57 }
58  
59 0 my $res;
60 0 while (1) {
61 0 $res = $self->SUPER::request($method, $url, $options);
62 0 ▶ if ($res->{status} !~ /\A[5]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->{"status"} =~ /\A[5]/) 0 0 0
63 0 $self->{_backoff}->success;
64 0 return $res;
65 }
66 0 my $secs = $self->{_backoff}->failure;
67 0 ▶ if ($secs == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($secs == -1) 0 0 0
68 log_trace "Failed requesting %s (%s - %s), giving up",
69 $url,
70 $res->{status},
71 0 $res->{reason};
72 0 return $res;
73 }
74 log_trace "Failed requesting %s (%s - %s), retrying in %.1f second(s) (attempt #%d) ...",
75 $url,
76 $res->{status},
77 $res->{reason},
78 $secs,
79 0 $self->{_backoff}{_attempts};
80 0 sleep $secs;
81 }
82 0 $res;
83 }
84  
85 1 1;
86 # ABSTRACT: Retry failed HTTP::Tiny requests
87  
88 __END__
↑ Summary