use std::time::{Duration, Instant};
pub trait Backoff {
fn backoff_period(&mut self, iterations: u32) -> Duration;
}
#[derive(Debug, Clone, Copy)]
pub struct ExponentialBackoff {
instant: Instant,
}
impl Backoff for ExponentialBackoff {
fn backoff_period(&mut self, iterations: u32) -> Duration {
let y = 1.25f32.powi(iterations as i32) - 1.0;
Duration::from_millis((y * 100.0) as u64)
}
}
impl Default for ExponentialBackoff {
fn default() -> Self {
Self {
instant: Instant::now(),
}
}
}
pub struct ImmediateBackoff;
impl Backoff for ImmediateBackoff {
fn backoff_period(&mut self, _iterations: u32) -> Duration {
Duration::from_secs(0)
}
}