|
NAMEAlgorithm::Backoff::Exponential - Backoff exponentially VERSIONThis document describes version 0.009 of Algorithm::Backoff::Exponential (from Perl distribution Algorithm-Backoff), released on 2019-06-20. SYNOPSIS use Algorithm::Backoff::Exponential;
# 1. instantiate
my $ab = Algorithm::Backoff::Exponential->new(
#consider_actual_delay => 1, # optional, default 0
#max_actual_duration => 0, # optional, default 0 (retry endlessly)
#max_attempts => 0, # optional, default 0 (retry endlessly)
#jitter_factor => 0.25, # optional, default 0
initial_delay => 5, # required
#max_delay => 100, # optional
#exponent_base => 2, # optional, default 2 (binary exponentiation)
#delay_on_success => 0, # optional, default 0
);
# 2. log success/failure and get a new number of seconds to delay, timestamp is
# optional but must be monotonically increasing.
# for example, using the parameters initial_delay=5, max_delay=100:
my $secs;
$secs = $ab->failure(); # => 5 (= initial_delay)
$secs = $ab->failure(); # => 10 (5 * 2^1)
$secs = $ab->failure(); # => 20 (5 * 2^2)
$secs = $ab->failure(); # => 33 (5 * 2^3 - 7)
$secs = $ab->failure(); # => 80 (5 * 2^4)
$secs = $ab->failure(); # => 100 ( min(5 * 2^5, 100) )
$secs = $ab->success(); # => 0 (= delay_on_success)
Illustration using CLI show-backoff-delays (10 failures followed by 3 successes): % show-backoff-delays -a Exponential --initial-delay 1 --max-delay 200 \
0 0 0 0 0 0 0 0 0 0 1 1 1
1
2
4
8
16
32
64
128
200
200
0
0
0
DESCRIPTIONThis backoff algorithm calculates the next delay as: initial_delay * exponent_base ** (attempts-1) Only the "initial_delay" is required. "exponent_base" is 2 by default (binary exponential). For the first failure attempt ("attempts" = 1) the delay equals the initial delay. Then it is doubled, quadrupled, and so on (using the default exponent base of 2). There are limits on the number of attempts (`max_attempts`) and total duration (`max_actual_duration`). It is recommended to add a jitter factor, e.g. 0.25 to add some randomness to avoid "thundering herd problem". METHODSnewUsage: new(%args) -> obj This function is not exported. Arguments ('*' denotes required arguments):
Return value: (obj) HOMEPAGEPlease visit the project's homepage at <https://metacpan.org/release/Algorithm-Backoff>. SOURCESource repository is at <https://github.com/perlancar/perl-Algorithm-Backoff>. BUGSPlease report any bugs or feature requests on the bugtracker website <https://rt.cpan.org/Public/Dist/Display.html?Name=Algorithm-Backoff> 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. SEE ALSO<https://en.wikipedia.org/wiki/Exponential_backoff> Algorithm::Backoff Other "Algorithm::Backoff::*" classes. AUTHORperlancar <perlancar@cpan.org> COPYRIGHT AND LICENSEThis 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.
|