Add ability to inspect upcoming sleep in stop
funcs, and add stop_before_delay
#423
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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).