| 1 | package Algorithm::Backoff; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 16 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 16 | ▶ | use strict 'subs', 'vars'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 16 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 16 | ▶ | use Time::HiRes qw(time); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 16 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||||||||||||||||
| 10 | 16 | our $DATE = '2024-02-24'; # DATE | |||||||||||||||||||||||||||||||||||||||||
| 11 | 16 | our $DIST = 'Algorithm-Backoff'; # DIST | |||||||||||||||||||||||||||||||||||||||||
| 12 | 16 | our $VERSION = '0.010'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 16 | our %SPEC; | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 16 | our %attr_consider_actual_delay = ( | |||||||||||||||||||||||||||||||||||||||||
| 17 | consider_actual_delay => { | ||||||||||||||||||||||||||||||||||||||||||
| 18 | summary => 'Whether to consider actual delay', | ||||||||||||||||||||||||||||||||||||||||||
| 19 | schema => ['bool*'], | ||||||||||||||||||||||||||||||||||||||||||
| 20 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 21 | tags => ['common'], | ||||||||||||||||||||||||||||||||||||||||||
| 22 | description => <<'_', | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | If set to true, will take into account the actual delay (timestamp difference). | ||||||||||||||||||||||||||||||||||||||||||
| 25 | For example, when using the Constant strategy of delay=2, you log failure() | ||||||||||||||||||||||||||||||||||||||||||
| 26 | again right after the previous failure() (i.e. specify the same timestamp). | ||||||||||||||||||||||||||||||||||||||||||
| 27 | failure() will then return ~2+2 = 4 seconds. On the other hand, if you waited 2 | ||||||||||||||||||||||||||||||||||||||||||
| 28 | seconds before calling failure() again (i.e. specify the timestamp that is 2 | ||||||||||||||||||||||||||||||||||||||||||
| 29 | seconds larger than the previous timestamp), failure() will return 2 seconds. | ||||||||||||||||||||||||||||||||||||||||||
| 30 | And if you waited 4 seconds or more, failure() will return 0. | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | _ | ||||||||||||||||||||||||||||||||||||||||||
| 33 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 16 | our %attr_max_actual_duration = ( | |||||||||||||||||||||||||||||||||||||||||
| 37 | max_actual_duration => { | ||||||||||||||||||||||||||||||||||||||||||
| 38 | summary => 'Maximum number of seconds for all of the attempts (0 means unlimited)', | ||||||||||||||||||||||||||||||||||||||||||
| 39 | schema => ['ufloat*'], | ||||||||||||||||||||||||||||||||||||||||||
| 40 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 41 | tags => ['common'], | ||||||||||||||||||||||||||||||||||||||||||
| 42 | description => <<'_', | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | If set to a positive number, will limit the number of seconds for all of the | ||||||||||||||||||||||||||||||||||||||||||
| 45 | attempts. This setting is used to limit the amount of time you are willing to | ||||||||||||||||||||||||||||||||||||||||||
| 46 | spend on a task. For example, when using the Exponential strategy of | ||||||||||||||||||||||||||||||||||||||||||
| 47 | initial_delay=3 and max_attempts=10, the delays will be 3, 6, 12, 24, ... If | ||||||||||||||||||||||||||||||||||||||||||
| 48 | failures are logged according to the suggested delays, and max_actual_duration | ||||||||||||||||||||||||||||||||||||||||||
| 49 | is set to 21 seconds, then the third failure() will return -1 instead of 24 | ||||||||||||||||||||||||||||||||||||||||||
| 50 | because 3+6+12 >= 21, even though max_attempts has not been exceeded. | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | _ | ||||||||||||||||||||||||||||||||||||||||||
| 53 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 16 | our %attr_max_attempts = ( | |||||||||||||||||||||||||||||||||||||||||
| 57 | max_attempts => { | ||||||||||||||||||||||||||||||||||||||||||
| 58 | summary => 'Maximum number consecutive failures before giving up', | ||||||||||||||||||||||||||||||||||||||||||
| 59 | schema => 'uint*', | ||||||||||||||||||||||||||||||||||||||||||
| 60 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 61 | tags => ['common'], | ||||||||||||||||||||||||||||||||||||||||||
| 62 | description => <<'_', | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 means to retry endlessly without ever giving up. 1 means to give up after a | ||||||||||||||||||||||||||||||||||||||||||
| 65 | single failure (i.e. no retry attempts). 2 means to retry once after a failure. | ||||||||||||||||||||||||||||||||||||||||||
| 66 | Note that after a success, the number of attempts is reset (as expected). So if | ||||||||||||||||||||||||||||||||||||||||||
| 67 | max_attempts is 3, and if you fail twice then succeed, then on the next failure | ||||||||||||||||||||||||||||||||||||||||||
| 68 | the algorithm will retry again for a maximum of 3 times. | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | _ | ||||||||||||||||||||||||||||||||||||||||||
| 71 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 16 | our %attr_jitter_factor = ( | |||||||||||||||||||||||||||||||||||||||||
| 75 | jitter_factor => { | ||||||||||||||||||||||||||||||||||||||||||
| 76 | summary => 'How much to add randomness', | ||||||||||||||||||||||||||||||||||||||||||
| 77 | schema => ['float*', between=>[0, 0.5]], | ||||||||||||||||||||||||||||||||||||||||||
| 78 | tags => ['common'], | ||||||||||||||||||||||||||||||||||||||||||
| 79 | description => <<'_', | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | If you set this to a value larger than 0, the actual delay will be between a | ||||||||||||||||||||||||||||||||||||||||||
| 82 | random number between original_delay * (1-jitter_factor) and original_delay * | ||||||||||||||||||||||||||||||||||||||||||
| 83 | (1+jitter_factor). Jitters are usually added to avoid so-called "thundering | ||||||||||||||||||||||||||||||||||||||||||
| 84 | herd" problem. | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | The jitter will be applied to delay on failure as well as on success. | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | _ | ||||||||||||||||||||||||||||||||||||||||||
| 89 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 16 | our %attr_delay_on_success = ( | |||||||||||||||||||||||||||||||||||||||||
| 93 | delay_on_success => { | ||||||||||||||||||||||||||||||||||||||||||
| 94 | summary => 'Number of seconds to wait after a success', | ||||||||||||||||||||||||||||||||||||||||||
| 95 | schema => 'ufloat*', | ||||||||||||||||||||||||||||||||||||||||||
| 96 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 97 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 16 | our %attr_max_delay = ( | |||||||||||||||||||||||||||||||||||||||||
| 101 | max_delay => { | ||||||||||||||||||||||||||||||||||||||||||
| 102 | summary => 'Maximum delay time, in seconds', | ||||||||||||||||||||||||||||||||||||||||||
| 103 | schema => 'ufloat*', | ||||||||||||||||||||||||||||||||||||||||||
| 104 | tags => ['common'], | ||||||||||||||||||||||||||||||||||||||||||
| 105 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 106 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 16 | our %attr_min_delay = ( | |||||||||||||||||||||||||||||||||||||||||
| 109 | min_delay => { | ||||||||||||||||||||||||||||||||||||||||||
| 110 | summary => 'Maximum delay time, in seconds', | ||||||||||||||||||||||||||||||||||||||||||
| 111 | schema => 'ufloat*', | ||||||||||||||||||||||||||||||||||||||||||
| 112 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 113 | tags => ['common'], | ||||||||||||||||||||||||||||||||||||||||||
| 114 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 16 | our %attr_initial_delay = ( | |||||||||||||||||||||||||||||||||||||||||
| 118 | initial_delay => { | ||||||||||||||||||||||||||||||||||||||||||
| 119 | summary => 'Initial delay for the first attempt after failure, '. | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 'in seconds', | ||||||||||||||||||||||||||||||||||||||||||
| 121 | schema => 'ufloat*', | ||||||||||||||||||||||||||||||||||||||||||
| 122 | req => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 123 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 124 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 16 | our %attr_delay_multiple_on_failure = ( | |||||||||||||||||||||||||||||||||||||||||
| 127 | delay_multiple_on_failure => { | ||||||||||||||||||||||||||||||||||||||||||
| 128 | summary => 'How much to multiple previous delay, upon failure (e.g. 1.5)', | ||||||||||||||||||||||||||||||||||||||||||
| 129 | schema => 'ufloat*', | ||||||||||||||||||||||||||||||||||||||||||
| 130 | req => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 131 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 132 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 16 | our %attr_delay_multiple_on_success = ( | |||||||||||||||||||||||||||||||||||||||||
| 135 | delay_multiple_on_success => { | ||||||||||||||||||||||||||||||||||||||||||
| 136 | summary => 'How much to multiple previous delay, upon success (e.g. 0.5)', | ||||||||||||||||||||||||||||||||||||||||||
| 137 | schema => 'ufloat*', | ||||||||||||||||||||||||||||||||||||||||||
| 138 | req => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 139 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 16 | our %attr_delay_increment_on_failure = ( | |||||||||||||||||||||||||||||||||||||||||
| 143 | delay_increment_on_failure => { | ||||||||||||||||||||||||||||||||||||||||||
| 144 | summary => 'How much to add to previous delay, in seconds, upon failure (e.g. 5)', | ||||||||||||||||||||||||||||||||||||||||||
| 145 | schema => 'float*', | ||||||||||||||||||||||||||||||||||||||||||
| 146 | req => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 147 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 148 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 16 | our %attr_delay_increment_on_success = ( | |||||||||||||||||||||||||||||||||||||||||
| 151 | delay_increment_on_success => { | ||||||||||||||||||||||||||||||||||||||||||
| 152 | summary => 'How much to add to previous delay, in seconds, upon success (e.g. -5)', | ||||||||||||||||||||||||||||||||||||||||||
| 153 | schema => 'float*', | ||||||||||||||||||||||||||||||||||||||||||
| 154 | req => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 155 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 156 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | $SPEC{new} = { | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 16 | v => 1.1, | |||||||||||||||||||||||||||||||||||||||||
| 160 | is_class_meth => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 161 | is_func => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 162 | args => { | ||||||||||||||||||||||||||||||||||||||||||
| 163 | %attr_max_attempts, | ||||||||||||||||||||||||||||||||||||||||||
| 164 | %attr_jitter_factor, | ||||||||||||||||||||||||||||||||||||||||||
| 165 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 166 | result_naked => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 167 | result => { | ||||||||||||||||||||||||||||||||||||||||||
| 168 | schema => 'obj*', | ||||||||||||||||||||||||||||||||||||||||||
| 169 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 170 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 19 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=23.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 19 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | 19 | my $attrspec = ${"$class\::SPEC"}{new}{args}; | |||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | # check known attributes | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 19 | for my $arg (keys %args) { | |||||||||||||||||||||||||||||||||||||||||
| 178 | 60 | ▶ | $arg =~ /\A(_start_timestamp)\z/ and next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 59 | ▶ | $attrspec->{$arg} or die "$class: Unknown attribute '$arg'"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | # check required attributes and set default | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 19 | for my $attr (keys %$attrspec) { | |||||||||||||||||||||||||||||||||||||||||
| 183 | 159 | ▶ | if ($attrspec->{$attr}{req}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 28 | ▶ | exists($args{$attr}) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | or die "$class: Missing required attribute '$attr'"; | ||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 159 | ▶ | if (exists $attrspec->{$attr}{default}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | 93 | ▶ | $args{$attr} //= $attrspec->{$attr}{default}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$args{$attr} //= $attrspec->{$attr}{"default"}
MC/DC0%0/2
$args{$attr} //= $attrspec->{$attr}{"default"}
$args{$attr}
$attrspec->{$attr}{"default"}
Truth table66%2/3
A //= B
A= $args{$attr}B= $attrspec->{$attr}{"default"}
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 19 | $args{_attempts} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 192 | 19 | ▶ | $args{_start_timestamp} //= time(); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$args{"_start_timestamp"} //= &Algorithm::Backoff::time()
MC/DC0%0/2
$args{"_start_timestamp"} //= &Algorithm::Backoff::time()
$args{"_start_timestamp"}
&Algorithm::Backoff::time()
Truth table66%2/3
A //= B
A= $args{"_start_timestamp"}B= &Algorithm::Backoff::time()
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 19 | bless \%args, $class; | |||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | 10 | ▶ | sub _consider_actual_delay { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_consider_actual_delay: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 10 | my ($self, $delay, $timestamp) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | 10 | ▶ | $self->{_prev_delay} //= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"_prev_delay"} //= 0
MC/DC100%1/1
$self->{"_prev_delay"} //= 0
$self->{"_prev_delay"}
Truth table100%2/2
A
A= $self->{"_prev_delay"}
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | 10 | my $actual_delay = $timestamp - $self->{_last_timestamp}; | |||||||||||||||||||||||||||||||||||||||||
| 201 | 10 | my $new_delay = $delay + $self->{_prev_delay} - $actual_delay; | |||||||||||||||||||||||||||||||||||||||||
| 202 | 10 | $new_delay; | |||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | 120 | ▶ | sub _add_jitter { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_add_jitter: called CC=3 SCAR=11.8
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 120 | my ($self, $delay) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 207 | 120 | ▶ | return $delay unless $delay && $self->{jitter_factor}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$delay and $self->{"jitter_factor"}
MC/DC0%0/2
$delay and $self->{"jitter_factor"}
$delay
$self->{"jitter_factor"}
Truth table33%1/3
A and B
A= $delayB= $self->{"jitter_factor"}
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | 120 | my $min = $delay * (1-$self->{jitter_factor}); | |||||||||||||||||||||||||||||||||||||||||
| 209 | 120 | my $max = $delay * (1+$self->{jitter_factor}); | |||||||||||||||||||||||||||||||||||||||||
| 210 | 120 | $min + ($max-$min)*rand(); | |||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | 205 | ▶ | sub _success_or_failure { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_success_or_failure: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | 205 | my ($self, $is_success, $timestamp) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | 205 | ▶ | $self->{_last_timestamp} //= $timestamp; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"_last_timestamp"} //= $timestamp
MC/DC0%0/2
$self->{"_last_timestamp"} //= $timestamp
$self->{"_last_timestamp"}
$timestamp
Truth table66%2/3
A //= B
A= $self->{"_last_timestamp"}B= $timestamp
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | $timestamp >= $self->{_last_timestamp} or | ||||||||||||||||||||||||||||||||||||||||||
| 218 | 205 | ▶ | die ref($self).": Decreasing timestamp ". | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | "($self->{_last_timestamp} -> $timestamp)"; | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | 204 | ▶ | my $delay = $is_success ? | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | $self->_success($timestamp) : $self->_failure($timestamp); | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | $delay = $self->_consider_actual_delay($delay, $timestamp) | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 204 | ▶ | if $self->{consider_actual_delay}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | $delay = $self->_add_jitter($delay) | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 204 | ▶ | if $self->{jitter_factor}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | # keep between max(0, min_delay) and max_delay | ||||||||||||||||||||||||||||||||||||||||||
| 231 | $delay = $self->{max_delay} | ||||||||||||||||||||||||||||||||||||||||||
| 232 | 204 | ▶ | if defined $self->{max_delay} && $delay > $self->{max_delay}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->{"max_delay"} and $delay > $self->{"max_delay"}
MC/DC100%2/2
defined $self->{"max_delay"} and $delay > $self->{"max_delay"}
defined $self->{"max_delay"}
$delay > $self->{"max_delay"}
Truth table100%3/3
A and B
A= defined $self->{"max_delay"}B= $delay > $self->{"max_delay"}
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 204 | ▶ | $delay = 0 if $delay < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | $delay = $self->{min_delay} | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 204 | ▶ | if defined $self->{min_delay} && $delay < $self->{min_delay}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"min_delay"} and $delay < $self->{"min_delay"}
MC/DC50%1/2
defined $self->{"min_delay"} and $delay < $self->{"min_delay"}
defined $self->{"min_delay"}
$delay < $self->{"min_delay"}
Truth table66%2/3
A and B
A= defined $self->{"min_delay"}B= $delay < $self->{"min_delay"}
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | 204 | $self->{_last_timestamp} = $timestamp; | |||||||||||||||||||||||||||||||||||||||||
| 238 | 204 | $self->{_prev_delay} = $delay; | |||||||||||||||||||||||||||||||||||||||||
| 239 | 204 | $delay; | |||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | 88 | ▶ | sub success { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
success: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | 88 | my ($self, $timestamp) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | 88 | ▶ | $timestamp //= time(); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$timestamp //= &Algorithm::Backoff::time()
MC/DC0%0/2
$timestamp //= &Algorithm::Backoff::time()
$timestamp
&Algorithm::Backoff::time()
Truth table66%2/3
A //= B
A= $timestampB= &Algorithm::Backoff::time()
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | 88 | $self->{_attempts} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | 88 | $self->_success_or_failure(1, $timestamp); | |||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||
| 252 | 121 | ▶ | sub failure { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
failure: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 121 | my ($self, $timestamp) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | 121 | ▶ | $timestamp //= time(); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$timestamp //= &Algorithm::Backoff::time()
MC/DC0%0/2
$timestamp //= &Algorithm::Backoff::time()
$timestamp
&Algorithm::Backoff::time()
Truth table66%2/3
A //= B
A= $timestampB= &Algorithm::Backoff::time()
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | return -1 if defined $self->{max_actual_duration} && | ||||||||||||||||||||||||||||||||||||||||||
| 258 | $self->{max_actual_duration} > 0 && | ||||||||||||||||||||||||||||||||||||||||||
| 259 | 121 | ▶ | $timestamp - $self->{_start_timestamp} >= $self->{max_actual_duration}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $self->{"max_actual_duration"} and $self->{"max_actual_duration"} > 0 and $timestamp - $self->{"_start_timestamp"} >= $self->{"max_actual_duration"}
defined $self->{"max_actual_duration"} and $self->{"max_actual_duration"} > 0
MC/DC66%2/3
defined $self->{"max_actual_duration"} and $self->{"max_actual_duration"} > 0 and $timestamp - $self->{"_start_timestamp"} >= $self->{"max_actual_duration"}
defined $self->{"max_actual_duration"}
$self->{"max_actual_duration"} > 0
$timestamp - $self->{"_start_timestamp"} >= $self->{"max_actual_duration"}
Truth table75%3/4
A and B and C
A= defined $self->{"max_actual_duration"}B= $self->{"max_actual_duration"} > 0C= $timestamp - $self->{"_start_timestamp"} >= $self->{"max_actual_duration"}
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | 120 | $self->{_attempts}++; | |||||||||||||||||||||||||||||||||||||||||
| 262 | return -1 if $self->{max_attempts} && | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 120 | ▶ | $self->{_attempts} >= $self->{max_attempts}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"max_attempts"} and $self->{"_attempts"} >= $self->{"max_attempts"}
MC/DC100%2/2
$self->{"max_attempts"} and $self->{"_attempts"} >= $self->{"max_attempts"}
$self->{"max_attempts"}
$self->{"_attempts"} >= $self->{"max_attempts"}
Truth table100%3/3
A and B
A= $self->{"max_attempts"}B= $self->{"_attempts"} >= $self->{"max_attempts"}
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | 117 | $self->_success_or_failure(0, $timestamp); | |||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | 16 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 269 | # ABSTRACT: Various backoff strategies for retry | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | __END__ | ||||||||||||||||||||||||||||||||||||||||||