use std::time::Duration;
pub type BackoffStrategy = Box<dyn Send + Fn(u32) -> Duration>;
pub fn exponential_backoff_strategy(error_count: u32) -> Duration {
Duration::from_secs(1_u64 << error_count.min(6))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_exponential_backoff_strategy() {
let cases = [
(1, Duration::from_secs(2)),
(5, Duration::from_secs(32)),
(42, Duration::from_secs(64)),
];
for (error_count, expected) in cases {
assert_eq!(exponential_backoff_strategy(error_count), expected);
}
}
}