Algorithm-Retry
view release on metacpan or search on metacpan
lib/Algorithm/Retry/ExponentialBackoff.pm view on Meta::CPAN
$secs = $ar->failure(); # => 33 (5 * 2^3 - 7)
$secs = $ar->failure(); # => 80 (5 * 2^4)
$secs = $ar->failure(); # => 100 ( min(5 * 2^5, 100) )
$secs = $ar->success(); # => 0 (= delay_on_success)
=head1 DESCRIPTION
This backoff algorithm calculates the next delay as:
initial_delay * exponent_base ** (attempts-1)
Only the C<initial_delay> is required. C<exponent_base> is 2 by default (binary
expoential). For the first failure attempt (C<attempts> = 1) the delay equals
the initial delay. Then it is doubled, quadrupled, and so on (using the default
exponent base of 2).
It is recommended to add a jitter factor, e.g. 0.25 to add some randomness.
=head1 METHODS
=head2 new
Usage:
new(%args) -> obj
This function is not exported.
Arguments ('*' denotes required arguments):
=over 4
=item * B<consider_actual_delay> => I<bool> (default: 0)
Whether to consider actual delay.
If set to true, will take into account the actual delay (timestamp difference).
For example, when using the Constant strategy of delay=2, you log failure()
again right after the previous failure() (i.e. specify the same timestamp).
failure() will then return ~2+2 = 4 seconds. On the other hand, if you waited 2
seconds before calling failure() again (i.e. specify the timestamp that is 2
seconds larger than the previous timestamp), failure() will return 2 seconds.
And if you waited 4 seconds or more, failure() will return 0.
=item * B<delay_on_success> => I<ufloat> (default: 0)
Number of seconds to wait after a success.
=item * B<exponent_base> => I<ufloat> (default: 2)
=item * B<initial_delay>* => I<ufloat>
Initial delay for the first attempt after failure, in seconds.
=item * B<jitter_factor> => I<float>
How much to add randomness.
If you set this to a value larger than 0, the actual delay will be between a
random number between original_delay * (1-jitter_factor) and original_delay *
(1+jitter_factor). Jitters are usually added to avoid so-called "thundering
herd" problem.
=item * B<max_attempts> => I<uint> (default: 0)
Maximum number consecutive failures before giving up.
0 means to retry endlessly without ever giving up. 1 means to give up after a
single failure (i.e. no retry attempts). 2 means to retry once after a failure.
Note that after a success, the number of attempts is reset (as expected). So if
max_attempts is 3, and if you fail twice then succeed, then on the next failure
the algorithm will retry again for a maximum of 3 times.
=item * B<max_delay> => I<ufloat>
Maximum delay time, in seconds.
=back
Return value: (obj)
=head1 HOMEPAGE
Please visit the project's homepage at L<https://metacpan.org/release/Algorithm-Retry>.
=head1 SOURCE
Source repository is at L<https://github.com/perlancar/perl-Algorithm-Retry>.
=head1 BUGS
Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Algorithm-Retry>
When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.
=head1 SEE ALSO
L<https://en.wikipedia.org/wiki/Exponential_backoff>
L<Algorithm::Retry>
Other C<Algorithm::Retry::*> classes.
=head1 AUTHOR
perlancar <perlancar@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2019 by perlancar@cpan.org.
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
( run in 2.012 seconds using v1.01-cache-2.11-cpan-ceb78f64989 )