Skip to content

Commit 9ebfc6e

Browse files
committed
feat: add FixedNumber backoff policy
1 parent 587e2da commit 9ebfc6e

1 file changed

Lines changed: 37 additions & 2 deletions

File tree

src/backoff.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ impl Backoff for Stop {
4242
}
4343
}
4444

45-
/// Contant is a backoff policy which always returns
45+
/// Constant is a backoff policy which always returns
4646
/// a constant duration.
4747
#[derive(Debug)]
4848
pub struct Constant {
4949
interval: Duration,
5050
}
5151

5252
impl Constant {
53-
/// Creates a new Constant backoff with `interval` contant
53+
/// Creates a new Constant backoff with `interval` constant
5454
/// backoff.
5555
pub fn new(interval: Duration) -> Constant {
5656
Constant { interval }
@@ -62,3 +62,38 @@ impl Backoff for Constant {
6262
Some(self.interval)
6363
}
6464
}
65+
66+
/// Backoff policy with a fixed number of retries with a constant interval.
67+
#[derive(Debug)]
68+
pub struct FixedNumber {
69+
interval: Duration,
70+
max_attempts: usize,
71+
current_attempt: usize,
72+
}
73+
74+
impl FixedNumber {
75+
/// Creates a new FixedNumber backoff with fixed number of retry attempts (`max_attempts`)
76+
/// and constant duration between them (`interval`)
77+
pub fn new(interval: Duration, max_attempts: usize) -> Self {
78+
Self {
79+
interval,
80+
max_attempts,
81+
current_attempt: 0,
82+
}
83+
}
84+
}
85+
86+
impl Backoff for FixedNumber {
87+
fn reset(&mut self) {
88+
self.current_attempt = 0;
89+
}
90+
91+
fn next_backoff(&mut self) -> Option<Duration> {
92+
if self.current_attempt < self.max_attempts - 1 {
93+
self.current_attempt += 1;
94+
Some(self.interval)
95+
} else {
96+
None
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)