Skip to main content

Crate subms_rate_limiter

Crate subms_rate_limiter 

Source
Expand description

Lock-free rate limiter using the GCRA (Generic Cell Rate Algorithm) formulation.

State is a single AtomicU64 holding tat_ns - the theoretical arrival time of the next slot. try_acquire reads tat, computes the new value (max(now, tat) + period), and CAS-loops it in. Rejects when the new tat would land more than burst_ns in the future.

use subms_rate_limiter::RateLimiter;

// 1000 permits/sec, allow bursts of 10.
let rl = RateLimiter::new(1000.0, 10);
assert!(rl.try_acquire());

Thread-safety: RateLimiter is Send + Sync and every method takes &self. Share one instance across threads behind an Arc; there is no interior lock and no &mut self path.

Full writeup, design notes and measured benchmarks: https://www.submillisecond.com/cookbook/recipes/subms-rate-limiter

Re-exports§

pub use features::clock::Clock;
pub use features::clock::SystemClock;
pub use features::clock::TestClock;
pub use features::distributed_backend::Backend;
pub use features::distributed_backend::DistributedLimiter;
pub use features::distributed_backend::InMemoryBackend;
pub use features::hierarchical::HierarchicalLimiter;
pub use features::keyed::KeyedRateLimiter;
pub use features::metrics::MeteredTokenBucket;
pub use features::metrics::MetricsSnapshot;
pub use features::token_bucket::TokenBucket;

Modules§

features
Opt-in feature catalog. Each submodule is gated by its own Cargo feature flag and adds a focused capability to the base GCRA / leaky-bucket limiter without bloating the core build.
recipe
SubMsRecipe impl. Behind the harness feature.

Structs§

RateLimiter
Lock-free token-bucket / GCRA rate limiter.

Enums§

Acquire
Outcome of RateLimiter::try_acquire_with_retry: a permit was granted, or the caller should wait at least Retry(d) before a retry will conform - the value for an HTTP Retry-After. Under contention the duration is a best-effort hint (another thread may take the slot first), the guarantee every lock-free rate limiter’s retry-after carries.