Algori...::LILD(3pm)

πŸ“¦
version 0.010 Β· 5 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

🌸 Algorithm::Backoff::LILD πŸ’…

okay bestie, welcome to the LILD era β€” that's Linear Increment, Linear Decrement backoff, and yes, it's exactly as iconic as it sounds. this page is Algorithm::Backoff::LILD, a 3pm manual page from the User Contributed Perl Documentation section. let's get into it 🫢


✨ VERSION ✨

this is version 0.010 of Algorithm::Backoff::LILD, which ships as part of the Perl distribution Algorithm-Backoff, and it dropped on 2024-02-24. she's fresh, she's stable, she's serving.


πŸŽ€ SYNOPSIS πŸŽ€

here's the whole vibe, literally copy-paste ready:

use Algorithm::Backoff::LILD;

# 1. instantiate

my $ab = Algorithm::Backoff::LILD->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
    min_delay              => 1, # optional, default 0
    #max_delay             => 100, # optional
    initial_delay              =>  3, # required
    delay_increment_on_failure =>  4, # required
    delay_increment_on_success => -5, # required
);

# 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=3,
# delay_increment_on_failure=4, delay_increment_on_success=-5, min_delay=1:

my $secs;
$secs = $ab->failure();   # => 3  (= initial_delay)
$secs = $ab->failure();   # => 7  (3 + 4)
$secs = $ab->failure();   # => 11 (7 + 4)
$secs = $ab->success();   # => 6  (11 - 5)
$secs = $ab->success();   # => 1  (6 - 5)
$secs = $ab->success();   # => 1  (max(1 - 5, 0, min_delay=1))
$secs = $ab->failure();   # => 5  (1 + 4)
lowkey obsessed with that last line, the way it just clamps back up. max(1 - 5, 0, min_delay=1) is doing so much work πŸ’€

and here's a lil illustration using the CLI show-backoff-delays β€” that's 3 failures followed by 4 successes, followed by 3 failures:

% show-backoff-delays -a LILD --initial-delay 3 --min-delay 1 \
    --delay-increment-on-failure 4 --delay-increment-on-success -5 \
    0 0 0   1 1 1 1  0 0 0
3
7
11
6
1
1
1
5
9
13

πŸ’– DESCRIPTION πŸ’–

so when things fail, this backoff algorithm figures out the next delay like this:

D1 = initial_delay
D2 = max(min(D1 + delay_increment_on_failure, max_delay), min_delay)
...

and when things succeed, the next delay goes like:

D1 = initial_delay
D2 = max(D1 + delay_increment_on_success, min_delay, initial_delay)
...

initial_delay, delay_increment_on_failure, and delay_increment_on_success are all required. no skipping, bestie, they're mandatory.

there are also limits on the number of attempts (that's max_attempts) and the total duration (that's max_actual_duration).

and ngl, it is recommended to add a jitter factor, like 0.25, to add some randomness β€” that's how you dodge the "thundering herd problem". iconic anti-herd behaviour, we love to see it 🐎✨


πŸ‘‘ METHODS πŸ‘‘

πŸ¦‹ new

usage:

new(%args) -> obj

this function is not exported. (sorry to the import girlies.)

arguments, where * means required:

  • consider_actual_delay => bool (default: 0) β€” whether to consider the actual delay.

if you set this to true, it will take into account the actual delay (the timestamp difference). for example, when using the Constant strategy of delay=2, you log failure() again right after the previous failure() (i.e. you 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. you 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. the math is mathing πŸ”₯

  • delay_increment_on_failure* => float β€” how much to add to previous delay, in seconds, upon failure (e.g. 5).
  • delay_increment_on_success* => float β€” how much to add to previous delay, in seconds, upon success (e.g. -5).
  • initial_delay* => ufloat β€” initial delay for the first attempt after failure, in seconds.
  • jitter_factor => 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 the so-called "thundering herd" problem.

the jitter will be applied to delay on failure as well as on success. both. equal opportunity jitter πŸ’«

  • max_actual_duration => ufloat (default: 0) β€” maximum number of seconds for all of the attempts (0 means unlimited).

if set to a positive number, it will limit the number of seconds for all of the attempts. this setting is used to limit the amount of time you are willing to spend on a task. for example, when using the Exponential strategy of initial_delay=3 and max_attempts=10, the delays will be 3, 6, 12, 24, .... if failures are logged according to the suggested delays, and max_actual_duration is set to 21 seconds, then the third failure() will return -1 instead of 24 because 3+6+12 >= 21, even though max_attempts has not been exceeded. she said time's up πŸ›‘

  • max_attempts => 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. the reset is real, the grind never stops πŸ’…

  • max_delay => ufloat β€” maximum delay time, in seconds.
  • min_delay => ufloat (default: 0) β€” maximum delay time, in seconds.
bestie, yes, the docs really do say "maximum delay time, in seconds" for min_delay too. that's what the page says, so that's what we're telling you. iykyk πŸ™ˆ

return value: (obj) β€” you get the object, obviously, that's the whole point ✨


🌈 HOMEPAGE 🌈

please visit the project's homepage at <https://metacpan.org/release/Algorithm-Backoff>.


⭐ SOURCE ⭐

source repository is at <https://github.com/perlancar/perl-Algorithm-Backoff>.


🧚 SEE ALSO 🧚

  • Algorithm::Backoff::LIMD
  • Algorithm::Backoff::MILD
  • Algorithm::Backoff::MIMD
  • Algorithm::Backoff
  • other Algorithm::Backoff::* classes.

the whole friend group, honestly πŸ’œ


πŸ’€ AUTHOR πŸ’€

perlancar <perlancar@cpan.org>


πŸ“ CONTRIBUTING πŸ“

to contribute, you can send patches by email/via RT, or send pull requests on GitHub. come through!

most of the time, you don't need to build the distribution yourself. you can simply modify the code, then test via:

% prove -l

if you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. any additional steps required beyond that are considered a bug and can be reported to me. so if it's giving extra steps, that's not a feature, that's a bug report, periodt 🫢


this software is copyright (c) 2024, 2019 by perlancar <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.


πŸ™ˆ BUGS πŸ™ˆ

please 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. receipts or it didn't happen, bestie 😭


and that's the whole page, no cap πŸ’–

mwah, xoxo β€” this was Algorithm::Backoff::LILD version 0.010, documented as perl v5.40.1 on 2025-05-04. go forth and back off (linearly) βœ¨πŸ’…

πŸ“¦ where this page comes from