pub struct TokenBucket { /* private fields */ }Expand description
Token bucket rate limiter
Implements the token bucket algorithm for rate limiting:
- Bucket starts full (capacity tokens)
- Each operation consumes one token
- Tokens refill at a fixed rate
- Operations blocked when bucket is empty
Implementations§
Source§impl TokenBucket
impl TokenBucket
Sourcepub fn new(
capacity: u32,
tokens_per_refill: u32,
refill_interval: Duration,
) -> TokenBucket
pub fn new( capacity: u32, tokens_per_refill: u32, refill_interval: Duration, ) -> TokenBucket
Create a new token bucket
Sourcepub fn try_acquire(&self) -> bool
pub fn try_acquire(&self) -> bool
Try to consume a token
Returns true if a token was available and consumed, false otherwise.
Sourcepub fn available_tokens(&self) -> u32
pub fn available_tokens(&self) -> u32
Get current available tokens
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for TokenBucket
impl RefUnwindSafe for TokenBucket
impl Send for TokenBucket
impl Sync for TokenBucket
impl Unpin for TokenBucket
impl UnsafeUnpin for TokenBucket
impl UnwindSafe for TokenBucket
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