Hydra::Math: add an exponential_backoff function

[?]
Aug 26, 2021, 4:54 PM
L32HJNV55D7IBEQ55CDDRGBFAUUUAWGBJXSNAMTWHR3SIDH45X4QC

Dependencies

  • [2] 2JJP7673 tests: move to t, allow `yath test` from root
  • [*] D5QIOJGP * Move everything up one directory.

Change contents

  • file addition: Math.pm (----------)
    [4.53]
    package Hydra::Math;
    use strict;
    use warnings;
    use List::Util qw(min);
    use Exporter 'import';
    our @EXPORT_OK = qw(exponential_backoff);
    =head2 exponential_backoff
    Calculates a number of seconds to wait before reattempting something.
    Arguments:
    =over 1
    =item C<$attempts>
    Integer number of attempts made.
    =back
    =cut
    sub exponential_backoff {
    my ($attempt) = @_;
    my $clamp = min(10, $attempt);
    return 2 ** $clamp;
    }
    1;
  • file addition: Math.t (----------)
    [2.697]
    use strict;
    use warnings;
    use Setup;
    use Hydra::Math qw(exponential_backoff);
    use Test2::V0;
    subtest "exponential_backoff" => sub {
    is(exponential_backoff(0), 1);
    is(exponential_backoff(1), 2);
    is(exponential_backoff(2), 4);
    is(exponential_backoff(9), 512);
    is(exponential_backoff(10), 1024);
    is(exponential_backoff(11), 1024, "we're clamped to 1024 seconds");
    is(exponential_backoff(11000), 1024, "we're clamped to 1024 seconds");
    };
    done_testing;