pub struct KeyedRateLimiter<B: RateLimitBackend + Clone> { /* private fields */ }Expand description
Per-key rate limiter that tracks separate rate limits for each key.
Each key gets its own RateLimiter instance with either a
custom quota (set via with_quota_for_key)
or the default quota.
§Example
use ratelimit::{KeyedRateLimiter, Quota, InMemoryBackend};
#[tokio::main]
async fn main() {
let backend = InMemoryBackend::new();
let limiter = KeyedRateLimiter::new(Quota::per_second(100), backend);
// Tighter limit for login endpoint
limiter.with_quota_for_key("login", Quota::per_second(10));
let result = limiter.check("192.168.1.1").await;
assert!(result.allowed);
}Implementations§
Source§impl<B: RateLimitBackend + Clone> KeyedRateLimiter<B>
impl<B: RateLimitBackend + Clone> KeyedRateLimiter<B>
Sourcepub fn new(default_quota: Quota, backend: B) -> Self
pub fn new(default_quota: Quota, backend: B) -> Self
Create a new keyed rate limiter with the given default quota and backend.
Sourcepub fn with_quota_for_key(&self, key: &str, quota: Quota)
pub fn with_quota_for_key(&self, key: &str, quota: Quota)
Override the rate limit quota for a specific key.
This replaces any existing limiter for the key. If the key
has not been seen yet, the next call to check
will use this quota instead of the default.
Sourcepub async fn check(&self, key: &str) -> RateLimitResult
pub async fn check(&self, key: &str) -> RateLimitResult
Check whether the caller identified by key is allowed to proceed.
Sourcepub fn check_sync(&self, key: &str) -> RateLimitResult
pub fn check_sync(&self, key: &str) -> RateLimitResult
Synchronous version of check.
Blocks the current thread until the check completes. Useful for synchronous frameworks (e.g. Actix, Rocket).
Auto Trait Implementations§
impl<B> !RefUnwindSafe for KeyedRateLimiter<B>
impl<B> Freeze for KeyedRateLimiter<B>
impl<B> Send for KeyedRateLimiter<B>
impl<B> Sync for KeyedRateLimiter<B>
impl<B> Unpin for KeyedRateLimiter<B>
impl<B> UnsafeUnpin for KeyedRateLimiter<B>
impl<B> UnwindSafe for KeyedRateLimiter<B>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more