hyperlane-plugin-rate-limit 23.3.2

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
/// Standard `RateLimit-Limit` response header name.
pub const HEADER_RATE_LIMIT_LIMIT: &str = "ratelimit-limit";

/// Standard `RateLimit-Remaining` response header name.
pub const HEADER_RATE_LIMIT_REMAINING: &str = "ratelimit-remaining";

/// Standard `RateLimit-Reset` response header name (seconds until window/burst reset).
pub const HEADER_RATE_LIMIT_RESET: &str = "ratelimit-reset";

/// Default maximum requests permitted per window.
pub const DEFAULT_RATE_LIMIT_REQUESTS: u64 = 100;

/// Default rate-limit window duration in milliseconds (60 seconds).
pub const DEFAULT_RATE_LIMIT_WINDOW_MS: u64 = 60_000;

/// Default burst capacity when not overridden.
pub const DEFAULT_RATE_LIMIT_BURST: u64 = 100;

/// Default maximum tracked distinct rate-limit keys across all shards.
pub const DEFAULT_RATE_LIMIT_MAX_KEYS: usize = 65_536;

/// Number of independent lock-striped shards (`64`, power of two).
pub(crate) const RATE_LIMIT_SHARD_COUNT: usize = 64;

/// Nanoseconds per millisecond conversion factor.
pub(crate) const NANOS_PER_MS: u64 = 1_000_000;

/// Nanoseconds per second conversion factor.
pub(crate) const NANOS_PER_SEC: u64 = 1_000_000_000;

/// Fallback key used when no peer address or forwarding header is available.
pub(crate) const FALLBACK_CLIENT_KEY: &str = "anonymous";

/// Maximum decimal digits needed to format any `u64` on the stack.
pub(crate) const U64_STACK_BUF_LEN: usize = 20;

/// Stack buffer capacity in bytes for formatting any IPv4 or IPv6 address without heap allocation.
pub(crate) const IP_STACK_BUF_LEN: usize = 64;

/// Maximum header name length lowercased on the stack before falling back to heap allocation.
pub(crate) const STACK_HEADER_NAME_LEN: usize = 64;

/// Separator character joining request path and client IP in composite rate-limit keys.
pub(crate) const KEY_PATH_IP_SEPARATOR: char = '|';

/// Comma delimiter separating addresses in `X-Forwarded-For` headers.
pub(crate) const XFF_COMMA_DELIMITER: char = ',';

/// Multiplicative 64-bit mixing constant for zero-allocation [`FxHasher`].
pub(crate) const FX_HASH_K: u64 = 0x517c_c1b7_2722_0a95;

/// ASCII zero digit byte.
pub(crate) const BYTE_ZERO: u8 = b'0';

/// ASCII dot byte used in IPv4 formatting.
pub(crate) const BYTE_DOT: u8 = b'.';

/// Two-digit ASCII decimal lookup pairs for `00..=99`.
pub(crate) const DEC_DIGITS_LUT: &[u8; 200] = b"00010203040506070809\
10111213141516171819\
20212223242526272829\
30313233343536373839\
40414243444546474849\
50515253545556575859\
60616263646566676869\
70717273747576777879\
80818283848586878889\
90919293949596979899";

/// Bit shift separating the 32-bit window id from the 32-bit counter in packed fixed-window mode.
pub(crate) const FIXED_WIN_PACK_SHIFT: u32 = 32;

/// Bitmask extracting the 32-bit request count in packed fixed-window mode.
pub(crate) const FIXED_WIN_COUNT_MASK: u64 = 0xFFFF_FFFF;