use std::time::Duration;
#[derive(Debug, Clone)]
pub enum RetryStrategy {
NoRetry,
Fixed {
delay: Duration,
max_attempts: usize,
},
ExponentialBackoff {
initial_delay: Duration,
max_delay: Duration,
max_attempts: usize,
multiplier: f64,
},
}
impl RetryStrategy {
pub fn fixed(delay: Duration, max_attempts: usize) -> Self {
Self::Fixed {
delay,
max_attempts,
}
}
pub fn exponential(
initial_delay: Duration,
max_delay: Duration,
max_attempts: usize,
multiplier: f64,
) -> Self {
Self::ExponentialBackoff {
initial_delay,
max_delay,
max_attempts,
multiplier,
}
}
pub fn get_delay(&self, attempt: usize) -> Option<Duration> {
match self {
Self::NoRetry => None,
Self::Fixed {
delay,
max_attempts,
} => {
if attempt < *max_attempts {
Some(*delay)
} else {
None
}
}
Self::ExponentialBackoff {
initial_delay,
max_delay,
max_attempts,
multiplier,
} => {
if attempt < *max_attempts {
let delay = Duration::from_secs_f64(
initial_delay.as_secs_f64() * multiplier.powi(attempt as i32),
);
Some(delay.min(*max_delay))
} else {
None
}
}
}
}
}