pub struct PerKeyRateLimiter { /* private fields */ }Expand description
Per-API-key token-bucket rate limiter.
A separate TokenBucket is lazily created for each distinct API key on
first use. Keys that have an entry in overrides get a bucket with the
specified (capacity, rate) pair; all others share default_capacity and
default_rate.
Concurrency model:
- The outer
RwLockguards theHashMapof buckets. Read-lock is taken for lookups; write-lock is acquired only on the first hit for a new key. - Each bucket is wrapped in a
Mutexso multiple concurrent requests for the same key do not race on the bucket’s mutable refill state.
Implementations§
Source§impl PerKeyRateLimiter
impl PerKeyRateLimiter
Sourcepub fn new(default_capacity: f64, default_rate: f64) -> Self
pub fn new(default_capacity: f64, default_rate: f64) -> Self
Create a limiter with the given default capacity and refill rate.
Sourcepub fn with_overrides(self, overrides: HashMap<String, (f64, f64)>) -> Self
pub fn with_overrides(self, overrides: HashMap<String, (f64, f64)>) -> Self
Attach per-key overrides: key → (capacity, rate).
Returns self for builder-pattern chaining.
Sourcepub fn check_key(&self, key: &str) -> bool
pub fn check_key(&self, key: &str) -> bool
Check if a request for key should be allowed.
Returns true if a token was successfully consumed, false if the
bucket is exhausted (caller should respond 429).
On the first call for a given key the bucket is lazy-inserted under
the write lock; subsequent calls use a read lock for O(1) lookup.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for PerKeyRateLimiter
impl RefUnwindSafe for PerKeyRateLimiter
impl Send for PerKeyRateLimiter
impl Sync for PerKeyRateLimiter
impl Unpin for PerKeyRateLimiter
impl UnsafeUnpin for PerKeyRateLimiter
impl UnwindSafe for PerKeyRateLimiter
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more