Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add max retry parameter to exponential backoff #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

snowp
Copy link

@snowp snowp commented Oct 13, 2022

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

@yaozongyou
Copy link

same problem here, is there any will to merge this pull request?

@yaozongyou
Copy link

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.

@aawsome
Copy link

aawsome commented Jun 30, 2023

Another workaround is to use a custom struct implementing Backoff, e.g.

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
      }
   }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Max retry count?
3 participants