pub struct RateLimiter { /* private fields */ }Expand description
Per-subject rate limiting.
Cheap to clone into an axum layer: the counters are behind an Arc, and
two clones are one table rather than two.
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(policy: RateLimit) -> Self
pub fn new(policy: RateLimit) -> Self
A limiter applying policy, holding DEFAULT_CAPACITY subjects.
Sourcepub fn with_capacity(policy: RateLimit, capacity: usize) -> Self
pub fn with_capacity(policy: RateLimit, capacity: usize) -> Self
A limiter holding at most capacity subjects before it sweeps.
capacity is never read as zero: a table that cannot hold a subject
would clear itself on every request and limit nobody.
Sourcepub fn check(&self, subject: &str) -> Verdict
pub fn check(&self, subject: &str) -> Verdict
Counts one request from subject and says whether it may proceed.
The clock is read here rather than passed in so a caller cannot
accidentally supply a stale Instant; check_at is
the same function with the clock supplied, and exists for tests.
Trait Implementations§
Source§impl Clone for RateLimiter
impl Clone for RateLimiter
Auto Trait Implementations§
impl Freeze for RateLimiter
impl RefUnwindSafe for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl UnsafeUnpin for RateLimiter
impl UnwindSafe for RateLimiter
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