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

feat: add FixedNumber policy #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 37 additions & 2 deletions src/backoff.rs
Original file line number Diff line number Diff line change
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,38 @@ impl Backoff for Constant {
Some(self.interval)
}
}

/// Backoff policy with a fixed number of retries with a constant interval.
#[derive(Debug)]
pub 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`)
pub 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
}
}
}