token-bucket 10.11.0

Zero-dependency, lock-free atomic GCRA, Fixed-Window, Token-Bucket, and 64-way sharded bounded keyed rate-limiting engine.
Documentation
  • Coverage
  • 100%
    28 out of 28 items documented0 out of 0 items with examples
  • Size
  • Source code size: 143.4 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.2 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates-dev/ctares
    2 1 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • vshengbro

token-bucket

Zero-Dependency, Lock-Free Atomic GCRA, Fixed-Window, Token-Bucket & 64-Way Sharded Keyed Rate Limiter

crates.io docs.rs Rust 2024 License: MIT

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/ — MonotonicClock nanosecond monotonic time source anchored at limiter creation
  • decision/ — RateLimitDecision outcome struct with as_tuple() and Duration helpers
  • error/ — RateLimitError configuration validation enum and Display / Error implementations
  • gcra/ — Single-AtomicU64 lock-free GCRA (GcraConfig, GcraCell, GcraLimiter)
  • fixed_window/ — Dynamically bit-packed single-AtomicU64 lock-free fixed window (FixedWindowConfig, FixedWindowCell, FixedWindowLimiter)
  • token_bucket/ — Dynamically bit-packed single-AtomicU64 lock-free token bucket with GCD step reduction and atomic refunds (TokenBucketConfig, TokenBucketCell, TokenBucketLimiter)
  • hasher/ — Internal zero-dependency 64-bit word-at-a-time seeded FastHasher with SplitMix64 avalanche
  • keyed/ — RateLimitPolicy and 64-way cache-line-aligned bounded KeyedRateLimiter<K> with idle-first LRU eviction

Installation & Quick Start

cargo add token-bucket
use token_bucket::*;

let limiter: GcraLimiter = GcraLimiter::per_second(100, 10).expect("valid config");
let decision: RateLimitDecision = limiter.check();
let (allowed, remaining, reset_after_ns, retry_after_ns): (bool, u64, u64, u64) =
    decision.as_tuple();
assert!(allowed);
assert_eq!(remaining, 9);
assert_eq!(retry_after_ns, 0);
assert!(reset_after_ns > 0);

let keyed: KeyedRateLimiter<String> =
    KeyedRateLimiter::new(RateLimitPolicy::Gcra(*limiter.get_config()), 10_000)
        .expect("valid keyed limiter");
assert!(keyed.check("203.0.113.42").is_allowed());

Verification

Run crate-scoped formatting, lint, and test checks from the workspace root:

cargo run -p crate-cli -- fmt --check
cargo clippy -p token-bucket --all-features --all-targets -- -D warnings
cargo test -p token-bucket --all-features

Documentation & Governance

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.