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::*;

/// Admission decision produced by `CircuitBreaker::try_acquire` / `try_acquire_at_ns`.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct CircuitDecision {
    /// Whether the call was admitted (`true`) or rejected (`false`).
    pub(crate) allowed: bool,
    /// Circuit state at the moment the decision was made.
    pub(crate) state: CircuitState,
    /// Monotonic state-transition generation tag associated with this permit.
    pub(crate) generation: u32,
    /// Nanoseconds remaining before cooldown expires (`0` when allowed or half-open saturated).
    pub(crate) retry_after_ns: u64,
    /// Whether this admitted call holds a `HalfOpen` recovery probe slot.
    pub(crate) is_probe: bool,
}

/// Point-in-time diagnostic snapshot of a `CircuitBreaker`.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct CircuitSnapshot {
    /// Current circuit state.
    pub(crate) state: CircuitState,
    /// Current state-transition generation.
    pub(crate) generation: u32,
    /// Successful calls recorded inside the active sliding window.
    pub(crate) window_successes: u64,
    /// Failed calls recorded inside the active sliding window.
    pub(crate) window_failures: u64,
    /// Current consecutive failure streak in `Closed` state.
    pub(crate) consecutive_failures: u16,
    /// Probes dispatched in the current `HalfOpen` cycle.
    pub(crate) half_open_dispatched: u16,
    /// Probes succeeded in the current `HalfOpen` cycle.
    pub(crate) half_open_succeeded: u16,
    /// Remaining open cooldown nanoseconds (`0` when not `Open` or cooldown elapsed).
    pub(crate) retry_after_ns: u64,
}

/// RAII permit guard that automatically records a failure on drop unless completed or cancelled.
#[derive(Debug)]
pub struct CircuitGuard<'a> {
    /// Reference to the owning `CircuitBreaker`.
    pub(crate) breaker: &'a CircuitBreaker,
    /// Permit decision carrying state and generation identity.
    pub(crate) decision: CircuitDecision,
    /// Whether the caller has already finalized or cancelled this guard.
    pub(crate) completed: bool,
}