pub struct TokenBucketRateLimiter { /* private fields */ }Expand description
Token bucket implementation for rate limiting.
The token bucket algorithm allows for burst traffic while maintaining a sustainable long-term rate. Tokens are added at a constant rate and consumed when requests arrive.
Implementations§
Source§impl TokenBucketRateLimiter
impl TokenBucketRateLimiter
Sourcepub fn new(capacity: usize, refill_rate: f64) -> Self
pub fn new(capacity: usize, refill_rate: f64) -> Self
Creates a new token bucket rate limiter.
§Arguments
capacity- Maximum number of tokens (burst capacity)refill_rate- Tokens added per second (sustained rate)
Sourcepub fn try_consume(&self, tokens: usize) -> bool
pub fn try_consume(&self, tokens: usize) -> bool
Tries to consume tokens from the bucket.
Returns true if tokens were available and consumed, false otherwise.
Sourcepub fn available_tokens(&self) -> f64
pub fn available_tokens(&self) -> f64
Returns the current number of available tokens.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for TokenBucketRateLimiter
impl RefUnwindSafe for TokenBucketRateLimiter
impl Send for TokenBucketRateLimiter
impl Sync for TokenBucketRateLimiter
impl Unpin for TokenBucketRateLimiter
impl UnwindSafe for TokenBucketRateLimiter
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: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).