add max retry parameter to exponential backoff#60
Open
snowp wants to merge 1 commit intoihrwein:masterfrom
Open
add max retry parameter to exponential backoff#60snowp wants to merge 1 commit intoihrwein:masterfrom
snowp wants to merge 1 commit intoihrwein:masterfrom
Conversation
|
same problem here, is there any will to merge this pull request? |
|
Currently without this commit, normal retry can be worked around this: fn retry<F, B, T, E>(max_attempts: usize, backoff: B, mut op: F) -> Result<T, backoff::Error<E>>
where
F: FnMut() -> Result<T, backoff::Error<E>>,
B: backoff::backoff::Backoff,
{
let mut attempt_count = 0;
backoff::retry(backoff, || {
let result = op();
attempt_count += 1;
if attempt_count >= max_attempts {
result.map_err(|err| match err {
backoff::Error::Permanent(err) => backoff::Error::permanent(err),
backoff::Error::Transient { err, .. } => backoff::Error::permanent(err),
})
} else {
result
}
})
}but with future retry, it is more complicated, i have tried to using backoff::future::retry_notify and backoff::Notify, record each attempt to Notify, if the record in notify exceeds maximum, return some permanent error, but in the end, it does not work out. |
|
Another workaround is to use a custom struct implementing struct ExpBackoffWithMaxRetries {
exp: ExponentialBackoff,
retries: usize,
}
impl Backoff for ExpBackoffWithMaxRetries {
fn reset(&mut self) {
self.exp.reset();
self.retries = 0;
}
fn next_backoff(&mut self) -> Option<Duration> {
self.retries += 1;
if self.retries >= MAX_RETRIES {
self.exp.next_backoff()
} else {
None
}
}
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds a parameter to the ExponentatialBackoff allowing for the maximum number of retires to be specified, allowing for both a max retry count as well as a max duration to be specified for the backoff.
Fixes #55