use std::time::Duration;
#[derive(Debug, Clone)]
pub(crate) struct IdleBackoff {
current: Duration,
initial: Duration,
max: Duration,
}
impl IdleBackoff {
pub(crate) fn exponential_with_cap(initial: Duration, max: Duration) -> Self {
Self {
current: Duration::ZERO,
initial,
max,
}
}
pub(crate) fn reset(&mut self) {
self.current = Duration::ZERO;
}
pub(crate) fn next_delay(&mut self) -> Duration {
if self.current == Duration::ZERO {
self.current = self.initial;
return self.current;
}
let doubled = self.current.checked_mul(2).unwrap_or(self.max);
self.current = std::cmp::min(doubled, self.max);
self.current
}
}