pub struct RateLimiter { /* private fields */ }Expand description
Rate limiter using the token bucket algorithm.
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(max_tokens: u32, refill_rate: f64) -> Self
pub fn new(max_tokens: u32, refill_rate: f64) -> Self
Create a new rate limiter.
max_tokens: Maximum number of tokens (burst capacity).refill_rate: Tokens added per second.
Sourcepub async fn check(&self, key: &str) -> bool
pub async fn check(&self, key: &str) -> bool
Check if a request from the given key is allowed.
Returns true if the request is allowed (token consumed),
false if rate-limited (429 should be returned).
Sourcepub fn too_many_requests() -> Result<Response<Full<Bytes>>, OxiHttpError>
pub fn too_many_requests() -> Result<Response<Full<Bytes>>, OxiHttpError>
Build a 429 Too Many Requests response.
Trait Implementations§
Source§impl Clone for RateLimiter
impl Clone for RateLimiter
Source§fn clone(&self) -> RateLimiter
fn clone(&self) -> RateLimiter
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for RateLimiter
impl !UnwindSafe for RateLimiter
impl Freeze for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl UnsafeUnpin 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