pub struct Builder<C: Clock + Clone = SystemClock> { /* private fields */ }Expand description
A fluent builder for a RateLimiter when the Tier-1 constructors are not
enough.
Start it with RateLimiter::builder, chain the knobs you care about —
algorithm, quota, burst, shard count, eviction policy, clock — and call
build. Anything left unset keeps a sane default: the token
bucket, default sharding, and the bounded-memory Eviction default. The
quota defaults to a limit of 0 (which denies everything), so set it.
build is infallible: a zero limit produces a deny-everything limiter, and a
zero period a degenerate one, rather than an error.
§Examples
use rate_net::{RateLimiter, Eviction};
use std::time::Duration;
let limiter = RateLimiter::builder()
.quota(1000, Duration::from_secs(60)) // 1000 / minute
.burst(50) // allow short bursts of 50
.shards(64) // tune for core count
.eviction(Eviction::idle(Duration::from_secs(300)))
.build();
assert_eq!(limiter.quota().limit(), 1000);
assert_eq!(limiter.quota().burst(), 50);
assert_eq!(limiter.shards(), 64);Implementations§
Source§impl<C: Clock + Clone> Builder<C>
impl<C: Clock + Clone> Builder<C>
Sourcepub fn algorithm(self, algorithm: Algorithm) -> Self
pub fn algorithm(self, algorithm: Algorithm) -> Self
Selects the algorithm. Defaults to Algorithm::TokenBucket. The leaky
bucket and window algorithms require the algorithms feature.
§Examples
use rate_net::{RateLimiter, Algorithm};
use std::time::Duration;
let limiter = RateLimiter::builder()
.algorithm(Algorithm::SlidingWindowCounter)
.quota(100, Duration::from_secs(1))
.build();
assert_eq!(limiter.algorithm(), Algorithm::SlidingWindowCounter);Sourcepub fn quota(self, limit: u32, period: Duration) -> Self
pub fn quota(self, limit: u32, period: Duration) -> Self
Sets the quota: limit requests per period, per key.
§Examples
use rate_net::RateLimiter;
use std::time::Duration;
let limiter = RateLimiter::builder().quota(5, Duration::from_millis(100)).build();
assert_eq!(limiter.quota().limit(), 5);Sourcepub fn per_second(self, limit: u32) -> Self
pub fn per_second(self, limit: u32) -> Self
Sets the quota to limit requests per second.
§Examples
use rate_net::RateLimiter;
let limiter = RateLimiter::builder().per_second(100).build();
assert_eq!(limiter.quota().limit(), 100);Sourcepub fn per_minute(self, limit: u32) -> Self
pub fn per_minute(self, limit: u32) -> Self
Sets the quota to limit requests per minute.
§Examples
use rate_net::RateLimiter;
use std::time::Duration;
let limiter = RateLimiter::builder().per_minute(600).build();
assert_eq!(limiter.quota().period(), Duration::from_secs(60));Sourcepub fn burst(self, burst: u32) -> Self
pub fn burst(self, burst: u32) -> Self
Sets the burst ceiling. Defaults to the quota’s limit. Applies to the token and leaky buckets; window algorithms ignore it.
§Examples
use rate_net::RateLimiter;
let limiter = RateLimiter::builder().per_second(100).burst(250).build();
assert_eq!(limiter.quota().burst(), 250);Sourcepub fn shards(self, shards: usize) -> Self
pub fn shards(self, shards: usize) -> Self
Sets the shard count (rounded up to a power of two). Defaults to a small multiple of the core count.
§Examples
use rate_net::RateLimiter;
let limiter = RateLimiter::builder().per_second(1).shards(128).build();
assert_eq!(limiter.shards(), 128);Sourcepub fn eviction(self, eviction: Eviction) -> Self
pub fn eviction(self, eviction: Eviction) -> Self
Sets the eviction policy. Defaults to a bounded-memory capacity cap.
§Examples
use rate_net::{RateLimiter, Eviction};
let limiter = RateLimiter::builder().per_second(1).eviction(Eviction::capacity(1_000)).build();
assert_eq!(limiter.eviction().max_keys(), Some(1_000));Sourcepub fn clock<C2: Clock + Clone>(self, clock: C2) -> Builder<C2>
pub fn clock<C2: Clock + Clone>(self, clock: C2) -> Builder<C2>
Sets the time source, for deterministic tests with a ManualClock.
§Examples
use rate_net::RateLimiter;
use clock_lib::ManualClock;
use std::sync::Arc;
let clock = Arc::new(ManualClock::new());
let limiter = RateLimiter::builder().per_second(5).clock(Arc::clone(&clock)).build();
assert!(limiter.check("k").is_allow());Sourcepub fn build(self) -> RateLimiter<C>
pub fn build(self) -> RateLimiter<C>
Builds the configured limiter.
§Examples
use rate_net::RateLimiter;
use std::time::Duration;
let limiter = RateLimiter::builder().quota(10, Duration::from_secs(1)).build();
assert!(limiter.check("k").is_allow());