token-bucket 11.1.0

Zero-dependency, lock-free atomic GCRA, Fixed-Window, Token-Bucket, and 64-way sharded bounded keyed rate-limiting engine.
Documentation
use super::*;

/// Accessors and tuple/duration conversions for `RateLimitDecision`.
impl RateLimitDecision {
    /// Constructs a new `RateLimitDecision`.
    ///
    /// # Arguments
    ///
    /// - `bool` - Whether the request was allowed.
    /// - `u64` - Remaining quota after evaluation.
    /// - `u64` - Nanoseconds until full quota reset/refill.
    /// - `u64` - Nanoseconds until the request may be retried.
    /// - `u64` - Configured burst or window limit.
    #[inline(always)]
    pub fn new(
        allowed: bool,
        remaining: u64,
        reset_after_ns: u64,
        retry_after_ns: u64,
        limit: u64,
    ) -> Self {
        Self {
            allowed,
            remaining,
            reset_after_ns,
            retry_after_ns,
            limit,
        }
    }

    /// Returns whether the request was allowed.
    ///
    /// # Arguments
    ///
    /// - `&Self` - The decision.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` if admitted, `false` if rate-limited.
    #[inline(always)]
    pub fn get_allowed(&self) -> bool {
        self.allowed
    }

    /// Alias for `get_allowed` returning whether the request was admitted.
    ///
    /// # Arguments
    ///
    /// - `&Self` - The decision.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` if admitted, `false` if rate-limited.
    #[inline(always)]
    pub fn is_allowed(&self) -> bool {
        self.get_allowed()
    }

    /// Returns remaining tokens or requests available after evaluation.
    ///
    /// # Arguments
    ///
    /// - `&Self` - The decision.
    ///
    /// # Returns
    ///
    /// - `u64` - Remaining quota units.
    #[inline(always)]
    pub fn get_remaining(&self) -> u64 {
        self.remaining
    }

    /// Returns nanoseconds until the quota fully resets or refills.
    ///
    /// # Arguments
    ///
    /// - `&Self` - The decision.
    ///
    /// # Returns
    ///
    /// - `u64` - Nanoseconds until full reset.
    #[inline(always)]
    pub fn get_reset_after_ns(&self) -> u64 {
        self.reset_after_ns
    }

    /// Returns nanoseconds until the caller should retry.
    ///
    /// # Arguments
    ///
    /// - `&Self` - The decision.
    ///
    /// # Returns
    ///
    /// - `u64` - `0` when allowed, retry wait in nanoseconds when denied.
    #[inline(always)]
    pub fn get_retry_after_ns(&self) -> u64 {
        self.retry_after_ns
    }

    /// Returns the configured limit or burst capacity.
    ///
    /// # Arguments
    ///
    /// - `&Self` - The decision.
    ///
    /// # Returns
    ///
    /// - `u64` - Configured burst or window limit.
    #[inline(always)]
    pub fn get_limit(&self) -> u64 {
        self.limit
    }

    /// Returns `(allowed, remaining, reset_after_ns, retry_after_ns)` as a tuple.
    ///
    /// # Arguments
    ///
    /// - `&Self` - The decision.
    ///
    /// # Returns
    ///
    /// - `(bool, u64, u64, u64)` - Compact decision tuple.
    #[inline(always)]
    pub fn as_tuple(&self) -> (bool, u64, u64, u64) {
        (
            self.get_allowed(),
            self.get_remaining(),
            self.get_reset_after_ns(),
            self.get_retry_after_ns(),
        )
    }

    /// Returns `reset_after_ns` as a `Duration`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - The decision.
    ///
    /// # Returns
    ///
    /// - `Duration` - Duration until full quota reset.
    #[inline(always)]
    pub fn reset_after(&self) -> Duration {
        Duration::from_nanos(self.get_reset_after_ns())
    }

    /// Returns `retry_after_ns` as a `Duration`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - The decision.
    ///
    /// # Returns
    ///
    /// - `Duration` - Duration until retry.
    #[inline(always)]
    pub fn retry_after(&self) -> Duration {
        Duration::from_nanos(self.get_retry_after_ns())
    }
}