pub struct TokenBucket { /* private fields */ }Expand description
Token bucket rate limiter
The token bucket allows for burst traffic up to the bucket capacity, while enforcing a sustained rate over time.
Implementations§
Source§impl TokenBucket
impl TokenBucket
Sourcepub fn new(capacity: u32, refill_rate: f64) -> Self
pub fn new(capacity: u32, refill_rate: f64) -> Self
Create a new token bucket
§Arguments
capacity- Maximum tokens (burst capacity)refill_rate- Tokens added per second
Sourcepub fn try_acquire(&self, tokens: u32) -> Result<(), TokenBucketExceeded>
pub fn try_acquire(&self, tokens: u32) -> Result<(), TokenBucketExceeded>
Try to acquire tokens
Returns Ok(()) if tokens were acquired, Err with retry info if not.
Sourcepub fn acquire_blocking(
&self,
tokens: u32,
timeout: Duration,
) -> Result<(), TokenBucketExceeded>
pub fn acquire_blocking( &self, tokens: u32, timeout: Duration, ) -> Result<(), TokenBucketExceeded>
Acquire tokens, blocking until available (with timeout)
Sourcepub fn return_tokens(&self, tokens: u32)
pub fn return_tokens(&self, tokens: u32)
Return tokens to the bucket (e.g., if operation was cancelled)
Sourcepub fn current_tokens(&self) -> u32
pub fn current_tokens(&self) -> u32
Get current token count
Sourcepub fn refill_rate(&self) -> f64
pub fn refill_rate(&self) -> f64
Get refill rate (tokens per second)
Sourcepub fn fill_ratio(&self) -> f64
pub fn fill_ratio(&self) -> f64
Get fill percentage (0.0 - 1.0)
Sourcepub fn set_capacity(&mut self, capacity: u32)
pub fn set_capacity(&mut self, capacity: u32)
Update capacity (for dynamic limits)
Sourcepub fn set_refill_rate(&mut self, rate: f64)
pub fn set_refill_rate(&mut self, rate: f64)
Update refill rate (for dynamic limits)
Trait Implementations§
Source§impl Clone for TokenBucket
impl Clone for TokenBucket
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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