1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
package Twitter::API::Trait::RetryOnError;
# ABSTRACT: Automatically retry API calls on error
$Twitter::API::Trait::RetryOnError::VERSION = '1.0006';
use Moo::Role;
use Time::HiRes;
use namespace::clean;
#pod =attr initial_retry_delay
#pod
#pod Amount of time to delay before the initial retry. Specified in fractional
#pod seconds. Default: 0.25 (250ms).
#pod
#pod =cut
has initial_retry_delay => (
is => 'rw',
default => sub { 0.250 }, # 250 milliseconds
);
#pod =attr max_retry_delay
#pod
#pod Maximum delay between retries, specified in fractional seconds. Default: 4.0.
#pod
#pod =cut
has max_retry_delay => (
is => 'rw',
default => sub { 4.0 }, # 4 seconds
);
#pod =attr retry_delay_multiplier
#pod
#pod After the initial delay, the delay time is multiplied by this factor to
#pod progressively back off allowing more time for the transient condition to
#pod resolve. However, the delay never exceeds C<max_retry_delay>. Default: 2.0.
#pod
#pod =cut
has retry_delay_multiplier => (
is => 'rw',
default => sub { 2 }, # double the prior delay
);
#pod =attr max_retries
#pod
#pod Maximum number of times to retry on error. Default: 5.
#pod
#pod =cut
has max_retries => (
is => 'rw',
default => sub { 5 }, # 0 = try forever
);
#pod =attr retry_delay_code
#pod
#pod A coderef, called to implement a delay. It takes a single parameter, the number
#pod of seconds to delay. E.g., 0.25. The default implementation is simply:
#pod
#pod sub { Time::HiRes::sleep(shift) }
#pod
#pod =cut
has retry_delay_code => (
is => 'rw',
default => sub {
sub { Time::HiRes::sleep(shift) };
},
);
around send_request => sub {
my $orig = shift;
my $self = shift;
my ( $c ) = @_;
my $msg = $c->http_request;
my $is_oauth = ( $msg->header('authorization') // '' ) =~ /^OAuth /;
my $delay = $self->initial_retry_delay;
my $retries = $self->max_retries;
my $res;
while () {
$res = $self->$orig(@_);
# return on success or permanent error
return $res if $res->code < 500 || $retries-- == 0;
$self->retry_delay_code->($delay);
$delay *= $self->retry_delay_multiplier;
$delay = $self->max_retry_delay if $delay > $self->max_retry_delay;
# If this is an OAuth request, we need a new Authorization header
# (the nonce may be invalid, now).
if ( $is_oauth ) {
$self->add_authorization($c);
}
}
$res;
};
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Twitter::API::Trait::RetryOnError - Automatically retry API calls on error
=head1 VERSION
version 1.0006
=head1 SYNOPSIS
use Twitter::API;
my $client = Twitter::API->new_with_options(
traits => [ qw/ApiMethods RetryOnError/ ],
%other_optons
);
my $statuses = $client->home_timeline;
=head1 DESCRIPTION
With this trait applied, Twitter::API automatically retries API calls that
result in an HTTP status code of 500 or greater. These errors often indicate a
temporary problem, either on Twitter's end, locally, or somewhere in between.
By default, it retries up to 5 times. The initial retry is delayed by 250ms.
Additional retries double the delay time until the maximum delay is reached
(default: 4 seconds). Twitter::API throws a C<Twitter::API::Error> exception
when it receives a permanent error (HTTP status code below 500), or the maximum
number of retries has been reached.
For non-blocking applications, set a suitable C<retry_delay_code> callback.
This attribute can also be used to provided retry logging.
=head1 ATTRIBUTES
=head2 initial_retry_delay
Amount of time to delay before the initial retry. Specified in fractional
seconds. Default: 0.25 (250ms).
=head2 max_retry_delay
Maximum delay between retries, specified in fractional seconds. Default: 4.0.
=head2 retry_delay_multiplier
After the initial delay, the delay time is multiplied by this factor to
progressively back off allowing more time for the transient condition to
resolve. However, the delay never exceeds C<max_retry_delay>. Default: 2.0.
=head2 max_retries
Maximum number of times to retry on error. Default: 5.
=head2 retry_delay_code
A coderef, called to implement a delay. It takes a single parameter, the number
of seconds to delay. E.g., 0.25. The default implementation is simply:
sub { Time::HiRes::sleep(shift) }
=head1 AUTHOR
Marc Mims <marc@questright.com>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2015-2021 by Marc Mims.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|