hyperlane-plugin-rate-limit 23.1.0

A 64-way sharded lock-free atomic GCRA and fixed-window rate limiter plugin for the Hyperlane framework with extensible KeyExtractor and RateLimitStore traits and RFC RateLimit headers.
Documentation
use super::*;

/// Result of evaluating one request against a [`RateLimitStore`].
#[derive(Clone, Copy, Data, Debug, Eq, PartialEq)]
pub struct RateLimitDecision {
    /// `true` when the request is admitted; `false` when throttled (`429 Too Many Requests`).
    #[get(type(copy))]
    pub(crate) allowed: bool,
    /// Effective quota limit for the window or burst.
    #[get(type(copy))]
    pub(crate) limit: u64,
    /// Remaining permitted requests right after this evaluation.
    #[get(type(copy))]
    pub(crate) remaining: u64,
    /// Seconds until the bucket fully resets.
    #[get(type(copy))]
    pub(crate) reset_after_secs: u64,
    /// Seconds the caller should wait before retrying (`0` when allowed).
    #[get(type(copy))]
    pub(crate) retry_after_secs: u64,
}

/// Configuration policy governing [`ShardedRateLimiter`] and [`RateLimitPlugin`].
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct RateLimitPolicy {
    /// Sustained requests allowed per `window_ms`.
    #[get(type(copy))]
    pub(crate) requests_per_window: u64,
    /// Window length in milliseconds.
    #[get(type(copy))]
    pub(crate) window_ms: u64,
    /// Maximum burst size for [`RateLimitAlgorithm::Gcra`].
    #[get(type(copy))]
    pub(crate) burst: u64,
    /// Enforcement algorithm (`Gcra` or `FixedWindow`).
    #[get(type(copy))]
    pub(crate) algorithm: RateLimitAlgorithm,
    /// Maximum tracked distinct keys before stale eviction triggers.
    #[get(type(copy))]
    pub(crate) max_keys: usize,
    /// Whether to emit `RateLimit-*` headers on responses.
    #[get(type(copy))]
    pub(crate) include_headers: bool,
    /// Exact paths bypassed by [`RateLimitHook`] (for example `"/healthz"`).
    pub(crate) exempt_paths: Vec<String>,
}

/// Zero-allocation multiplicative 64-bit hasher for rate-limit shard tables.
#[derive(Clone, Copy, Default)]
pub(crate) struct FxHasher {
    /// Running 64-bit hash accumulator.
    pub(crate) state: u64,
}

/// Precomputed Granlund-Montgomery fast unsigned 64-bit divider avoiding runtime `udiv` instructions.
#[derive(Clone, Copy)]
pub(crate) struct FastDiv64 {
    /// Original divisor (`>= 1`).
    pub(crate) divisor: u64,
    /// Magic high multiplier (`0` when `divisor` is a power of two).
    pub(crate) mult: u64,
    /// Post-shift amount (`trailing_zeros` for powers of two, `l - 1` otherwise).
    pub(crate) shift: u32,
    /// Whether `divisor` is a power of two (`n >> shift` fast path).
    pub(crate) is_pow2: bool,
}

/// Cache-line-aligned rate-limiter shard preventing cross-thread false sharing.
#[repr(align(64))]
pub(crate) struct RateLimitShard {
    /// Per-shard read-write lock guarding bucket entries.
    pub(crate) map: RwLock<RateLimitShardMap>,
}

/// Lock-free atomic state for one partition key inside [`ShardedRateLimiter`].
pub(crate) struct BucketEntry {
    /// GCRA theoretical arrival time (ns) or fixed-window packed `(window_id, count)` / window index.
    pub(crate) primary: AtomicU64,
    /// Fixed-window overflow request count (used only when window or limit exceeds `u32::MAX`).
    pub(crate) counter: AtomicU64,
    /// Monotonic last-access timestamp in nanoseconds since limiter epoch.
    pub(crate) last_seen_ns: AtomicU64,
}

/// Shared interior state of [`ShardedRateLimiter`].
pub(crate) struct ShardedRateLimiterInner {
    /// Active policy parameters.
    pub(crate) policy: RateLimitPolicy,
    /// Monotonic origin timestamp for nanosecond arithmetic.
    pub(crate) epoch: Instant,
    /// Precomputed GCRA emission interval in nanoseconds (`window_ns / requests_per_window`).
    pub(crate) emission_interval_ns: u64,
    /// Fast reciprocal divider for `emission_interval_ns`.
    pub(crate) emission_div: FastDiv64,
    /// Precomputed GCRA burst tolerance in nanoseconds (`emission_interval_ns * burst`).
    pub(crate) burst_tolerance_ns: u64,
    /// Precomputed window duration in nanoseconds.
    pub(crate) window_ns: u64,
    /// Fast reciprocal divider for `window_ns`.
    pub(crate) window_div: FastDiv64,
    /// Minimum nanoseconds elapsed before refreshing `BucketEntry::last_seen_ns` to avoid dirtying cache lines.
    pub(crate) last_seen_step_ns: u64,
    /// Lock-free count of tracked keys across all 64 shards.
    pub(crate) key_count: AtomicUsize,
    /// 64-way cache-aligned lock-striped key tables.
    pub(crate) shards: Box<[RateLimitShard; RATE_LIMIT_SHARD_COUNT]>,
}

/// High-concurrency 64-way sharded lock-free in-memory [`RateLimitStore`].
#[derive(Clone)]
pub struct ShardedRateLimiter {
    /// Shared limiter state.
    pub(crate) inner: Arc<ShardedRateLimiterInner>,
}

/// Built-in [`KeyExtractor`] partitioning traffic by client IP address.
#[derive(Clone, Copy, Data, Debug, Default, Eq, PartialEq)]
pub struct ClientIpKeyExtractor {
    /// When `true`, inspects `X-Forwarded-For` and `X-Real-Ip` before falling back to peer IP.
    #[get(type(copy))]
    pub(crate) trust_proxy_headers: bool,
}

/// Built-in [`KeyExtractor`] partitioning traffic by a specific request header value (for example `X-Api-Key`).
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct HeaderKeyExtractor {
    /// Request header name read as the bucket key.
    pub(crate) header_name: String,
    /// Whether to fall back to client IP when the header is missing.
    #[get(type(copy))]
    pub(crate) fallback_to_ip: bool,
}

/// Built-in [`KeyExtractor`] partitioning traffic by `(request_path, client_ip)`.
#[derive(Clone, Copy, Data, Debug, Default, Eq, PartialEq)]
pub struct PathAndIpKeyExtractor {
    /// Whether forwarding headers are trusted when resolving the client IP.
    #[get(type(copy))]
    pub(crate) trust_proxy_headers: bool,
}

/// Stack-backed `fmt::Write` cursor for zero-allocation IPv6 formatting.
pub(crate) struct StackFmtWriter<'a> {
    /// Destination byte buffer.
    pub(crate) buf: &'a mut [u8],
    /// Current number of written UTF-8 bytes.
    pub(crate) len: usize,
}

/// Process-wide [`RateLimitPlugin`] registry storing policy, [`RateLimitStore`], and [`KeyExtractor`].
#[derive(Clone)]
pub struct RateLimitPlugin {
    /// Active rate-limiting policy.
    pub(crate) policy: RateLimitPolicy,
    /// Backend store evaluating quota decisions.
    pub(crate) store: Arc<dyn RateLimitStore>,
    /// Optional direct [`ShardedRateLimiter`] handle bypassing `dyn RateLimitStore` virtual dispatch.
    pub(crate) fast_store: Option<ShardedRateLimiter>,
    /// Preformatted default `RateLimit-Limit` header value.
    pub(crate) preformatted_limit: String,
    /// Expected limit value matching `preformatted_limit`.
    pub(crate) expected_limit: u64,
    /// Partition key extractor.
    pub(crate) key_extractor: Arc<dyn KeyExtractor>,
    /// When `true`, uses the zero-allocation stack IP extraction fast path directly.
    pub(crate) use_default_ip_fast_path: bool,
}

/// Opt-in [`ServerHook`] enforcing configured rate limits and attaching RFC `RateLimit-*` headers.
#[derive(Clone, Copy, Debug, Default)]
#[hyperlane::request_middleware(-95)]
pub struct RateLimitHook;