pub struct RateLimiterMap<K> { /* private fields */ }Expand description
A per-key rate limiter map.
Each key can be assigned its own rate and interval via insert_rate.
Keys that have not been inserted are rejected by default — call
insert_rate or insert_rate_interval first.
§Example
use load_balancer::rate_limit::RateLimiterMap;
use std::time::Duration;
#[tokio::main]
async fn main() {
let map = RateLimiterMap::new();
// alice: 3 req/s, bob: 10 req/s
map.insert_rate("alice", 3);
map.insert_rate_interval("bob", 10, Duration::from_secs(1));
assert!(map.check(&"alice").await);
assert!(map.check(&"bob").await);
assert!(!map.check(&"charlie").await); // not inserted → rejected
}Implementations§
Source§impl<K> RateLimiterMap<K>
impl<K> RateLimiterMap<K>
Sourcepub fn insert_rate(&self, key: K, rate: u64)
pub fn insert_rate(&self, key: K, rate: u64)
Insert a rate limiter for key with the given rate and a 1-second interval.
Overwrites any existing limiter for the same key.
Sourcepub fn insert_rate_interval(&self, key: K, rate: u64, interval: Duration)
pub fn insert_rate_interval(&self, key: K, rate: u64, interval: Duration)
Insert a rate limiter for key with the given rate and interval.
Sourcepub fn remove(&self, key: &K)
pub fn remove(&self, key: &K)
Remove the rate limiter for key.
After removal the key is unknown and will be rejected by
check (same as a key that was never inserted).
Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Returns true if key has an active rate limiter.
Sourcepub fn retain(&self, f: impl FnMut(&K, &mut RateLimiter) -> bool)
pub fn retain(&self, f: impl FnMut(&K, &mut RateLimiter) -> bool)
Retain only the keys for which f returns true.
Each remaining key’s limiter is unchanged.
Sourcepub fn entry(&self, key: K) -> Entry<'_, K, RateLimiter>
pub fn entry(&self, key: K) -> Entry<'_, K, RateLimiter>
Get an Entry for key, allowing in-place insertion or modification.
Use entry(key).or_insert(limiter) to insert a limiter only if the key
is absent, or entry(key).and_modify(...) to adjust an existing one.
Sourcepub async fn check(&self, key: &K) -> bool
pub async fn check(&self, key: &K) -> bool
Check whether key is allowed right now.
Returns true if the key’s limiter has available tokens.
Returns false if the key has not been inserted (unknown keys are
rejected by default).
Sourcepub async fn check_opt(&self, key: &K) -> Option<bool>
pub async fn check_opt(&self, key: &K) -> Option<bool>
Check key and distinguish “unknown” from “rate-limited”.
Returns Some(true) if allowed, Some(false) if rate-limited,
None if the key has not been inserted.
Sourcepub async fn time_until_available(&self, key: &K) -> Duration
pub async fn time_until_available(&self, key: &K) -> Duration
Returns the duration until key becomes available again.
Returns Duration::MAX if the key is not inserted or the rate is zero,
Duration::ZERO if tokens are immediately available.
Trait Implementations§
Source§impl<K: Clone> Clone for RateLimiterMap<K>
impl<K: Clone> Clone for RateLimiterMap<K>
Source§fn clone(&self) -> RateLimiterMap<K>
fn clone(&self) -> RateLimiterMap<K>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more