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.
hopefully closes #102
Putting this up for feedback. Happy to write tests/docs/etc.. after getting validation for the approach!
The basic idea is to add a new decorator,
on_retry_value
which takes a callable (rather than a generator). The callable takes as input the return value of the decorated method. The return value of the callable is the duration of the wait. All other params (max_wait, on_giveup, etc..) work as expected.This allows us to handle situations where backoff depends on the return value of the method e.g: if a
requests.Response
object contains aretry-after
type of header.A point to highlight: This solution uses a callable rather than a generator function. This is a departure from the current patterns. However, it felt like a generator doesn't quite fit the mold here as we need to pass the return value every time we call the decorated method.
How do we achieve this now, you might ask?
We engage in forbidden necromancy and bloodmagic whereby we use the
on_exception
decorator withconstant
generator with value 0, then raise an exception (essentially) containing the length of time we should wait. Then in theon_backoff
handler we read the value from the exception andsleep
. theremustbeabetterway.gif