Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "maybe-backoff"
version = "0.5.0"
version = "0.5.1"
authors = [
"Tibor Benke <ihrwein@gmail.com>",
"Aleksey Kuznetsov <zummenix@gmail.com>",
Expand Down
20 changes: 20 additions & 0 deletions src/maybe_backoff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(),
)
}
Expand All @@ -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());
}
}
Loading