token-bucket
Zero-Dependency, Lock-Free Atomic GCRA, Fixed-Window, Token-Bucket & 64-Way Sharded Keyed Rate Limiter
Workspace Overview · API Docs · Agent Engineering Rules
Overview
Provides a standalone, zero-dependency, lock-free atomic rate-limiting engine implementing GCRA (Generic Cell Rate Algorithm), Fixed-Window, and Token-Bucket limiters alongside a 64-way cache-line-aligned sharded bounded keyed limiter (KeyedRateLimiter). Every hot-path check() and check_n() call executes in nanoseconds with zero heap allocations and returns exact (allowed, remaining, reset_after_ns, retry_after_ns) timings.
This package is part of the ctares unified Rust 2024 workspace and follows the repository's strict module purity, formatting, and dependency-ordered release standards.
Responsibilities & Module Layout
clock/—MonotonicClocknanosecond monotonic time source anchored at limiter creationdecision/—RateLimitDecisionoutcome struct withas_tuple()andDurationhelperserror/—RateLimitErrorconfiguration validation enum andDisplay/Errorimplementationsgcra/— Single-AtomicU64lock-free GCRA (GcraConfig,GcraCell,GcraLimiter)fixed_window/— Dynamically bit-packed single-AtomicU64lock-free fixed window (FixedWindowConfig,FixedWindowCell,FixedWindowLimiter)token_bucket/— Dynamically bit-packed single-AtomicU64lock-free token bucket with GCD step reduction and atomic refunds (TokenBucketConfig,TokenBucketCell,TokenBucketLimiter)hasher/— Internal zero-dependency 64-bit word-at-a-time seededFastHasherwith SplitMix64 avalanchekeyed/—RateLimitPolicyand 64-way cache-line-aligned boundedKeyedRateLimiter<K>with idle-first LRU eviction
Installation & Quick Start
use *;
let limiter: GcraLimiter = per_second.expect;
let decision: RateLimitDecision = limiter.check;
let : =
decision.as_tuple;
assert!;
assert_eq!;
assert_eq!;
assert!;
let keyed: =
new
.expect;
assert!;
Verification
Run crate-scoped formatting, lint, and test checks from the workspace root:
Documentation & Governance
- Update this
README.mdalongside any public API, CLI flag, or module-boundary change per../AGENTS.md. - Project-level standards and release workflows live in
../.agents/skills/ctares/SKILL.md.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contact
For inquiries, please contact the author at root@devhub.vip.