diff --git a/Cargo.lock b/Cargo.lock index 4716b98..3728a45 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -890,7 +890,7 @@ dependencies = [ [[package]] name = "maybe-backoff" -version = "0.5.0" +version = "0.5.1" dependencies = [ "async-std", "futures-core", diff --git a/Cargo.toml b/Cargo.toml index 65fd751..86522e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "maybe-backoff" -version = "0.5.0" +version = "0.5.1" authors = [ "Tibor Benke ", "Aleksey Kuznetsov ", diff --git a/src/maybe_backoff.rs b/src/maybe_backoff.rs index 5003c36..95614d7 100644 --- a/src/maybe_backoff.rs +++ b/src/maybe_backoff.rs @@ -42,6 +42,10 @@ pub struct MaybeBackoff { } impl MaybeBackoff { + /// Starts the backoff unless it is already armed. An armed `MaybeBackoff` + /// always waits and never runs out: unlike the `ExponentialBackoff` + /// default, it has no 15-minute limit, so loops that retry forever keep + /// waiting between attempts. pub fn arm(&mut self) { if self.backoff.is_none() { self.backoff = Some( @@ -50,6 +54,7 @@ impl MaybeBackoff { .with_max_interval(Duration::from_secs(3)) .with_multiplier(1.5) .with_randomization_factor(0.2) + .with_max_elapsed_time(None) .build(), ) } @@ -71,3 +76,18 @@ impl MaybeBackoff { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn armed_backoff_keeps_waiting_after_fifteen_minutes() { + let mut maybe_backoff = MaybeBackoff::default(); + maybe_backoff.arm(); + let backoff = maybe_backoff.backoff.as_mut().unwrap(); + // `ExponentialBackoff` stops returning delays after 15 minutes by default. + backoff.start_time -= Duration::from_secs(16 * 60); + assert!(backoff.next_backoff().is_some()); + } +}