use std::time::Duration;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Backoff {
Fixed { interval: Duration },
Exponential { base: Duration, cap: Duration },
}
impl Backoff {
pub const fn fixed(interval: Duration) -> Self {
Self::Fixed { interval }
}
pub const fn exponential(base: Duration, cap: Duration) -> Self {
Self::Exponential { base, cap }
}
pub fn delay_before_attempt(&self, attempt: u32) -> Duration {
match self {
Self::Fixed { interval } => *interval,
Self::Exponential { base, cap } => {
let exponent = attempt.saturating_sub(1).min(31);
let multiplier = 1_u128 << exponent;
let millis = base
.as_millis()
.saturating_mul(multiplier)
.min(cap.as_millis());
Duration::from_millis(millis as u64)
}
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RetryPolicy {
max_attempts: u32,
backoff: Backoff,
}
impl RetryPolicy {
pub const fn new(max_attempts: u32, backoff: Backoff) -> Self {
Self {
max_attempts,
backoff,
}
}
pub const fn attempts_only(max_attempts: u32) -> Self {
Self::new(max_attempts, Backoff::fixed(Duration::from_secs(0)))
}
pub const fn max_attempts(&self) -> u32 {
self.max_attempts
}
pub const fn backoff(&self) -> Backoff {
self.backoff
}
pub const fn should_retry(&self, failed_attempts: u32) -> bool {
failed_attempts < self.max_attempts
}
pub fn delay_before_attempt(&self, attempt: u32) -> Duration {
self.backoff.delay_before_attempt(attempt)
}
pub fn next_attempt_count(attempts: u32) -> u32 {
attempts.saturating_add(1)
}
pub fn next_retry_ts(&self, finished_ts: u64, failed_attempts: u32) -> Option<u64> {
self.should_retry(failed_attempts).then(|| {
let seconds = self
.delay_before_attempt(failed_attempts.saturating_add(1))
.as_secs();
finished_ts.saturating_add(seconds)
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exponential_matches_legacy_binance_schedule() {
let backoff = Backoff::exponential(Duration::from_secs(1), Duration::from_secs(30));
assert_eq!(backoff.delay_before_attempt(1), Duration::from_secs(1));
assert_eq!(backoff.delay_before_attempt(2), Duration::from_secs(2));
assert_eq!(backoff.delay_before_attempt(5), Duration::from_secs(16));
assert_eq!(backoff.delay_before_attempt(6), Duration::from_secs(30));
assert_eq!(backoff.delay_before_attempt(64), Duration::from_secs(30));
assert_eq!(backoff.delay_before_attempt(0), Duration::from_secs(1));
let large = Backoff::exponential(Duration::from_millis(700), Duration::from_secs(10));
assert_eq!(large.delay_before_attempt(33), Duration::from_secs(10));
}
#[test]
fn fixed_matches_legacy_scheduler_schedule() {
let backoff = Backoff::fixed(Duration::from_secs(10));
for attempt in [0, 1, 2, 9, u32::MAX] {
assert_eq!(
backoff.delay_before_attempt(attempt),
Duration::from_secs(10)
);
}
}
#[test]
fn attempt_boundaries_and_terminal_match_legacy_sites() {
let scheduler = RetryPolicy::new(3, Backoff::fixed(Duration::from_secs(10)));
assert!(scheduler.should_retry(1));
assert!(scheduler.should_retry(2));
assert!(!scheduler.should_retry(3));
assert_eq!(scheduler.next_retry_ts(100, 2), Some(110));
assert_eq!(scheduler.next_retry_ts(100, 3), None);
assert_eq!(scheduler.next_retry_ts(u64::MAX - 1, 1), Some(u64::MAX));
let reconnect = RetryPolicy::new(
10,
Backoff::exponential(Duration::from_secs(1), Duration::from_secs(30)),
);
assert!(reconnect.should_retry(0));
assert!(reconnect.should_retry(9));
assert!(!reconnect.should_retry(10));
assert_eq!(reconnect.delay_before_attempt(10), Duration::from_secs(30));
assert_eq!(RetryPolicy::next_attempt_count(u32::MAX - 1), u32::MAX);
assert_eq!(RetryPolicy::next_attempt_count(u32::MAX), u32::MAX);
}
}