atomic-circuit-breaker 11.2.0

Zero-dependency, lock-free atomic sliding-window circuit breaker with CAS probe gating, monotonic clock support, and RAII guards.
Documentation
use super::*;

/// Cache-line-aligned wrapper isolating atomic state from cross-core false sharing.
#[derive(Debug)]
#[repr(align(128))]
pub(crate) struct CachePadded<T> {
    /// Wrapped value aligned to a 128-byte cache-line boundary.
    pub(crate) value: T,
}

/// Precomputed Granlund-Montgomery multiplier and shifts for fast unsigned `u64` division.
#[derive(Clone, Copy, Debug)]
pub(crate) struct FastDivU64 {
    /// Magic 64-bit reciprocal multiplier (`0` for power-of-two divisors).
    pub(crate) mul: u64,
    /// First post-subtraction right shift (`0` for simple/pow2 modes, else `1`).
    pub(crate) sh1: u32,
    /// Final right shift count.
    pub(crate) sh2: u32,
    /// Fast dispatch mode (`0` = power-of-two shift, `1` = single `umulh >> sh2`, `2` = 65-bit).
    pub(crate) mode: u8,
}

/// Inline 16-bucket SoA lock-free atomic sliding window isolating striped successes from failures.
#[derive(Debug)]
pub(crate) struct SlidingWindow {
    /// Precomputed reciprocal divider for `now_ns / bucket_ns`.
    pub(crate) fast_div: FastDivU64,
    /// Duration of one ring bucket in nanoseconds (`>= 1`).
    pub(crate) bucket_ns: u64,
    /// Four cache-line-isolated 16-slot success stripes eliminating RMW pipeline stalls.
    pub(crate) successes: [CachePadded<[AtomicU64; SLIDING_WINDOW_BUCKETS]>; SUCCESS_STRIPE_COUNT],
    /// Cache-line-isolated 16-slot failure ring buffer (`128` bytes total).
    pub(crate) failures: CachePadded<[AtomicU64; SLIDING_WINDOW_BUCKETS]>,
}