use core::time::Duration;
use reliakit_backoff::Backoff;
#[derive(Debug, Clone, Copy)]
pub struct RetryPolicy {
max_attempts: u32,
backoff: Backoff,
}
impl RetryPolicy {
pub const fn new(max_attempts: u32, backoff: Backoff) -> Option<Self> {
if max_attempts == 0 {
return None;
}
Some(Self {
max_attempts,
backoff,
})
}
pub const fn single(backoff: Backoff) -> Self {
Self {
max_attempts: 1,
backoff,
}
}
pub const fn max_attempts(&self) -> u32 {
self.max_attempts
}
pub const fn backoff(&self) -> &Backoff {
&self.backoff
}
pub fn delay_before_retry(&self, completed_attempts: u32) -> Duration {
let retry_index = completed_attempts.saturating_sub(1);
self.backoff.delay(retry_index).unwrap_or(Duration::ZERO)
}
}