| 1 | package HTTP::Tiny::Patch::Retry; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | our $DATE = '2018-10-06'; # DATE | |||||||||
| 4 | 1 | our $VERSION = '0.001'; # 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 Module::Patch qw(); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use base qw(Module::Patch); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 1 | our %config; | |||||||||
| 15 | |||||||||||
| 16 | 0 | ▶ | my $p_request = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||
| 17 | 0 | my $ctx = shift; | |||||||||
| 18 | 0 | my $orig = $ctx->{orig}; | |||||||||
| 19 | |||||||||||
| 20 | 0 | my ($self, $method, $url) = @_; | |||||||||
| 21 | |||||||||||
| 22 | 0 | my $retries = 0; | |||||||||
| 23 | 0 | my $res; | |||||||||
| 24 | 0 | while (1) { | |||||||||
| 25 | 0 | $res = $orig->(@_); | |||||||||
| 26 | 0 | ▶ | return $res if $res->{status} !~ /\A[5]/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 0 | ▶ | last if $retries >= $config{-retries}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 0 | $retries++; | |||||||||
| 29 | log_trace "Failed requesting $url ($res->{status} - $res->{reason}), retrying" . | ||||||||||
| 30 | 0 | ▶ | ($config{-delay} ? " in $config{-delay} second(s)" : "") . | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | " ($retries of $config{-retries}) ..."; | ||||||||||
| 32 | 0 | sleep $config{-delay}; | |||||||||
| 33 | } | ||||||||||
| 34 | 0 | $res; | |||||||||
| 35 | 1 | }; | |||||||||
| 36 | |||||||||||
| 37 | 0 | ▶ | sub patch_data { | ||||||||
|
Subroutine
patch_data: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 38 | return { | ||||||||||
| 39 | 0 | v => 3, | |||||||||
| 40 | config => { | ||||||||||
| 41 | -delay => { | ||||||||||
| 42 | summary => 'Number of seconds to wait between retries', | ||||||||||
| 43 | schema => 'nonnegint*', | ||||||||||
| 44 | default => 2, | ||||||||||
| 45 | }, | ||||||||||
| 46 | -retries => { | ||||||||||
| 47 | summary => 'Maximum number of retries to perform consecutively on a request (0=disable retry)', | ||||||||||
| 48 | schema => 'nonnegint*', | ||||||||||
| 49 | default => 3, | ||||||||||
| 50 | }, | ||||||||||
| 51 | }, | ||||||||||
| 52 | patches => [ | ||||||||||
| 53 | { | ||||||||||
| 54 | action => 'wrap', | ||||||||||
| 55 | mod_version => qr/^0\.*/, | ||||||||||
| 56 | sub_name => 'request', | ||||||||||
| 57 | code => $p_request, | ||||||||||
| 58 | }, | ||||||||||
| 59 | ], | ||||||||||
| 60 | }; | ||||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 1 | 1; | |||||||||
| 64 | # ABSTRACT: Retry failed HTTP::Tiny requests | ||||||||||
| 65 | |||||||||||
| 66 | __END__ | ||||||||||