use crate::strategy::BackoffStrategy;
use std::{num::NonZeroU32, time::Duration};
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Linear {
constant: Duration,
limit: NonZeroU32,
}
impl Linear {
pub fn new(constant: Duration, limit: NonZeroU32) -> Self {
Linear { constant, limit }
}
}
impl BackoffStrategy for Linear {
fn interval(&self, attempts: u32) -> Option<Duration> {
Some(self.constant.mul_f64(attempts as f64))
}
fn limit(&self) -> NonZeroU32 {
self.limit
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Constant {
constant: Duration,
limit: NonZeroU32,
}
impl BackoffStrategy for Constant {
fn interval(&self, _attempts: u32) -> Option<Duration> {
Some(self.constant)
}
fn limit(&self) -> NonZeroU32 {
self.limit
}
}
impl Constant {
pub fn new(constant: Duration, limit: NonZeroU32) -> Self {
Constant { constant, limit }
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Exponential {
constant: Duration,
factor: NonZeroU32,
limit: NonZeroU32,
}
impl Exponential {
pub fn new(constant: Duration, factor: NonZeroU32, limit: NonZeroU32) -> Self {
Exponential {
constant,
factor,
limit,
}
}
}
impl BackoffStrategy for Exponential {
fn interval(&self, attempts: u32) -> Option<Duration> {
Some(self.constant.mul_f64((self.factor.get() * attempts) as f64))
}
fn limit(&self) -> NonZeroU32 {
self.limit
}
}