Skip to content

Commit

Permalink
feat: add FixedNumber backoff policy
Browse files Browse the repository at this point in the history
  • Loading branch information
DDtKey committed Jul 22, 2022
1 parent 587e2da commit 154c883
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/backoff.rs
Expand Up @@ -42,15 +42,15 @@ impl Backoff for Stop {
}
}

/// Contant is a backoff policy which always returns
/// Constant is a backoff policy which always returns
/// a constant duration.
#[derive(Debug)]
pub struct Constant {
interval: Duration,
}

impl Constant {
/// Creates a new Constant backoff with `interval` contant
/// Creates a new Constant backoff with `interval` constant
/// backoff.
pub fn new(interval: Duration) -> Constant {
Constant { interval }
Expand All @@ -62,3 +62,37 @@ impl Backoff for Constant {
Some(self.interval)
}
}

/// Backoff policy with a fixed number of retries with a constant interval.
struct FixedNumber {
interval: Duration,
max_attempts: usize,
current_attempt: usize,
}

impl FixedNumber {
/// Creates a new FixedNumber backoff with fixed number of retry attempts (`max_attempts`)
/// and constant duration between them (`interval`)
fn new(interval: Duration, max_attempts: usize) -> Self {
Self {
interval,
max_attempts,
current_attempt: 0,
}
}
}

impl Backoff for FixedNumber {
fn reset(&mut self) {
self.current_attempt = 0;
}

fn next_backoff(&mut self) -> Option<Duration> {
if self.current_attempt < self.max_attempts - 1 {
self.current_attempt += 1;
Some(self.interval)
} else {
None
}
}
}

0 comments on commit 154c883

Please sign in to comment.