Skip to main content

Module rate_limiter

Module rate_limiter 

Source
Expand description

Token-bucket rate limiter.

Tokens accrue at rate_per_second up to burst. Acquiring a token blocks until one is available. Bursts are absorbed up to the bucket capacity, which is the right shape for protecting downstreams that can briefly tolerate traffic spikes but reject sustained overload.

The implementation is lock-free for the read path (one Mutex guarding a tiny token count + last-refill timestamp). For workloads >> 10k rps consider a sharded variant.

let limiter = RateLimiter::new(10.0, 5); // 10 rps, burst 5
for _ in 0..3 {
    limiter.acquire().await;
}

Structsยง

RateLimiter
Token-bucket rate limiter. Cheap to clone; the inner state is Arc<Mutex<_>>.