minerva 0.2.0

Causal ordering for distributed systems
use super::{Backoff, spin};

/// A backoff driven by a precomputed table of spin counts.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModelBasedBackoff {
    spins: &'static [u32],
}

impl ModelBasedBackoff {
    /// A backoff reading its spin count per attempt from `spins`.
    ///
    /// Entry `i` serves attempt `i + 1`; once `attempt` runs past the table the
    /// last entry repeats. An empty table is a valid no-op. The counts are relative
    /// contention knobs, not wall-clock durations.
    #[must_use]
    pub const fn new(spins: &'static [u32]) -> Self {
        Self { spins }
    }
}

impl Backoff for ModelBasedBackoff {
    fn backoff(&self, attempt: u32) {
        if self.spins.is_empty() {
            return;
        }
        let idx = (attempt.saturating_sub(1) as usize).min(self.spins.len() - 1);
        spin(self.spins[idx]);
    }
}