File Coverage

blib/lib/Algorithm/Backoff/Constant.pm
Criterion Covered Total %
statement 13 13 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 18 100.0


line stmt bran cond sub pod time code
1             package Algorithm::Backoff::Constant;
2              
3 3     3   573612 use strict;
  3         4  
  3         96  
4 3     3   11 use warnings;
  3         5  
  3         204  
5              
6 3     3   794 use parent qw(Algorithm::Backoff);
  3         655  
  3         17  
7              
8             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
9             our $DATE = '2024-02-24'; # DATE
10             our $DIST = 'Algorithm-Backoff'; # DIST
11             our $VERSION = '0.010'; # VERSION
12              
13             our %SPEC;
14              
15             $SPEC{new} = {
16             v => 1.1,
17             is_class_meth => 1,
18             is_func => 0,
19             args => {
20             %Algorithm::Backoff::attr_consider_actual_delay,
21             %Algorithm::Backoff::attr_max_actual_duration,
22             %Algorithm::Backoff::attr_max_attempts,
23             %Algorithm::Backoff::attr_jitter_factor,
24             %Algorithm::Backoff::attr_delay_on_success,
25             %Algorithm::Backoff::attr_min_delay,
26             %Algorithm::Backoff::attr_max_delay,
27             delay => {
28             summary => 'Number of seconds to wait after a failure',
29             schema => 'ufloat*',
30             req => 1,
31             },
32             },
33             result_naked => 1,
34             result => {
35             schema => 'obj*',
36             },
37             };
38              
39             sub _success {
40 65     65   119 my ($self, $timestamp) = @_;
41 65         144 $self->{delay_on_success};
42             }
43              
44             sub _failure {
45 82     82   151 my ($self, $timestamp) = @_;
46 82         201 $self->{delay};
47             }
48              
49             1;
50             # ABSTRACT: Backoff using a constant delay
51              
52             __END__
53              
54             =pod
55              
56             =encoding UTF-8
57              
58             =head1 NAME
59              
60             Algorithm::Backoff::Constant - Backoff using a constant delay
61              
62             =head1 VERSION
63              
64             This document describes version 0.010 of Algorithm::Backoff::Constant (from Perl distribution Algorithm-Backoff), released on 2024-02-24.
65              
66             =head1 SYNOPSIS
67              
68             use Algorithm::Backoff::Constant;
69              
70             # 1. instantiate
71              
72             my $ab = Algorithm::Backoff::Constant->new(
73             #consider_actual_delay => 1, # optional, default 0
74             #max_actual_duration => 0, # optional, default 0 (retry endlessly)
75             #max_attempts => 0, # optional, default 0 (retry endlessly)
76             #jitter_factor => 0, # optional, set to positive value to add randomness
77             delay => 2, # required
78             #delay_on_success => 0, # optional, default 0
79             );
80              
81             # 2. log success/failure and get a new number of seconds to delay, timestamp is
82             # optional argument (default is current time) but must be monotonically
83             # increasing.
84              
85             my $secs = $ab->failure(1554652553); # => 2
86             my $secs = $ab->success(); # => 0
87             my $secs = $ab->failure(); # => 2
88              
89             Illustration using CLI L<show-backoff-delays> (5 failures followed by 3
90             successes):
91              
92             % show-backoff-delays -a Constant --delay 2 \
93             0 0 0 0 0 1 1 1
94             2
95             2
96             2
97             2
98             2
99             0
100             0
101             0
102              
103             =head1 DESCRIPTION
104              
105             This backoff strategy is one of the simplest: it waits X second(s) after each
106             failure, or Y second(s) (default 0) after a success. There are limits on the
107             number of attempts (`max_attempts`) and total duration (`max_actual_duration`).
108             Some randomness can be introduced to avoid "thundering herd problem".
109              
110             =head1 METHODS
111              
112              
113             =head2 new
114              
115             Usage:
116              
117             new(%args) -> obj
118              
119             This function is not exported.
120              
121             Arguments ('*' denotes required arguments):
122              
123             =over 4
124              
125             =item * B<consider_actual_delay> => I<bool> (default: 0)
126              
127             Whether to consider actual delay.
128              
129             If set to true, will take into account the actual delay (timestamp difference).
130             For example, when using the Constant strategy of delay=2, you log failure()
131             again right after the previous failure() (i.e. specify the same timestamp).
132             failure() will then return ~2+2 = 4 seconds. On the other hand, if you waited 2
133             seconds before calling failure() again (i.e. specify the timestamp that is 2
134             seconds larger than the previous timestamp), failure() will return 2 seconds.
135             And if you waited 4 seconds or more, failure() will return 0.
136              
137             =item * B<delay>* => I<ufloat>
138              
139             Number of seconds to wait after a failure.
140              
141             =item * B<delay_on_success> => I<ufloat> (default: 0)
142              
143             Number of seconds to wait after a success.
144              
145             =item * B<jitter_factor> => I<float>
146              
147             How much to add randomness.
148              
149             If you set this to a value larger than 0, the actual delay will be between a
150             random number between original_delay * (1-jitter_factor) and original_delay *
151             (1+jitter_factor). Jitters are usually added to avoid so-called "thundering
152             herd" problem.
153              
154             The jitter will be applied to delay on failure as well as on success.
155              
156             =item * B<max_actual_duration> => I<ufloat> (default: 0)
157              
158             Maximum number of seconds for all of the attempts (0 means unlimited).
159              
160             If set to a positive number, will limit the number of seconds for all of the
161             attempts. This setting is used to limit the amount of time you are willing to
162             spend on a task. For example, when using the Exponential strategy of
163             initial_delay=3 and max_attempts=10, the delays will be 3, 6, 12, 24, ... If
164             failures are logged according to the suggested delays, and max_actual_duration
165             is set to 21 seconds, then the third failure() will return -1 instead of 24
166             because 3+6+12 >= 21, even though max_attempts has not been exceeded.
167              
168             =item * B<max_attempts> => I<uint> (default: 0)
169              
170             Maximum number consecutive failures before giving up.
171              
172             0 means to retry endlessly without ever giving up. 1 means to give up after a
173             single failure (i.e. no retry attempts). 2 means to retry once after a failure.
174             Note that after a success, the number of attempts is reset (as expected). So if
175             max_attempts is 3, and if you fail twice then succeed, then on the next failure
176             the algorithm will retry again for a maximum of 3 times.
177              
178             =item * B<max_delay> => I<ufloat>
179              
180             Maximum delay time, in seconds.
181              
182             =item * B<min_delay> => I<ufloat> (default: 0)
183              
184             Maximum delay time, in seconds.
185              
186              
187             =back
188              
189             Return value: (obj)
190              
191             =head1 HOMEPAGE
192              
193             Please visit the project's homepage at L<https://metacpan.org/release/Algorithm-Backoff>.
194              
195             =head1 SOURCE
196              
197             Source repository is at L<https://github.com/perlancar/perl-Algorithm-Backoff>.
198              
199             =head1 SEE ALSO
200              
201             L<Algorithm::Backoff>
202              
203             Other C<Algorithm::Backoff::*> classes.
204              
205             =head1 AUTHOR
206              
207             perlancar <perlancar@cpan.org>
208              
209             =head1 CONTRIBUTING
210              
211              
212             To contribute, you can send patches by email/via RT, or send pull requests on
213             GitHub.
214              
215             Most of the time, you don't need to build the distribution yourself. You can
216             simply modify the code, then test via:
217              
218             % prove -l
219              
220             If you want to build the distribution (e.g. to try to install it locally on your
221             system), you can install L<Dist::Zilla>,
222             L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
223             L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
224             Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
225             that are considered a bug and can be reported to me.
226              
227             =head1 COPYRIGHT AND LICENSE
228              
229             This software is copyright (c) 2024, 2019 by perlancar <perlancar@cpan.org>.
230              
231             This is free software; you can redistribute it and/or modify it under
232             the same terms as the Perl 5 programming language system itself.
233              
234             =head1 BUGS
235              
236             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Algorithm-Backoff>
237              
238             When submitting a bug or request, please include a test-file or a
239             patch to an existing test-file that illustrates the bug or desired
240             feature.
241              
242             =cut