This seeks to solve a specific problem: if using a wait function like wait_exponential or wait_random_exponential, along with stop_after_delay, the overall time spent retrying is >= max_delay.
However, in situations where you have a strict deadline and you can't block for longer than max_delay, it may be preferable to abort 1 retry attempt early so that you don't exceed that deadline.
This PR seeks to achieve that behavior by implementing stop_before_delay, which stops retries if the next attempt would take place after the max_delay time has elapsed, thereby ensuring that max_delay is never surpassed.
Examples of problem using exponential delays, and a 5 second deadline w/ stop_after_delay:
Note the same thing w/ random exponential backoff. In this case the final attempt is made 11.49 seconds after the first one, despite a 5 second max_delay. That's over 2x the amount of time spent blocking on retries than our strict deadline allows!
This situation with random exponential backoff is the one that I'm interested in improving upon for my own use case (avoiding thundering herds during connection retries from many parallel clients). This extra time spent waiting after max_delay can be quite long with higher amounts exponential backoff. eg. a supposedly 30s max_delay that actually blocks for over 1 minute can cause some confusion.
With stop_before, the max_delay is never exceeded:
This seeks to solve a specific problem: if using a wait function like
wait_exponential
orwait_random_exponential
, along withstop_after_delay
, the overall time spent retrying is >=max_delay
.However, in situations where you have a strict deadline and you can't block for longer than
max_delay
, it may be preferable to abort 1 retry attempt early so that you don't exceed that deadline.This PR seeks to achieve that behavior by implementing
stop_before_delay
, which stops retries if the next attempt would take place after themax_delay
time has elapsed, thereby ensuring thatmax_delay
is never surpassed.Examples of problem using exponential delays, and a 5 second deadline w/
stop_after_delay
:Note how the execution timestamp is 7 seconds after the initial one, despite the 5 second
max_delay
.Note the same thing w/ random exponential backoff. In this case the final attempt is made 11.49 seconds after the first one, despite a 5 second
max_delay
. That's over 2x the amount of time spent blocking on retries than our strict deadline allows!This situation with random exponential backoff is the one that I'm interested in improving upon for my own use case (avoiding thundering herds during connection retries from many parallel clients). This extra time spent waiting after
max_delay
can be quite long with higher amounts exponential backoff. eg. a supposedly 30smax_delay
that actually blocks for over 1 minute can cause some confusion.With
stop_before
, themax_delay
is never exceeded:Total elapsed time is < 5 seconds (3 seconds).
Total elapsed time is < 5 seconds (4.35).