pub struct TokenBucketLimiter { /* private fields */ }Expand description
Token bucket rate limiter
Implements the token bucket algorithm for rate limiting API calls. Tokens are added at a fixed rate, and each request consumes tokens. If insufficient tokens are available, the request is rate limited.
Implementations§
Source§impl TokenBucketLimiter
impl TokenBucketLimiter
Sourcepub fn new(tokens_per_second: f64, max_tokens: f64) -> Self
pub fn new(tokens_per_second: f64, max_tokens: f64) -> Self
Create a new token bucket limiter
§Arguments
tokens_per_second- Rate at which tokens are added (e.g., 10 for 10 requests/sec)max_tokens- Maximum tokens in bucket (burst capacity)
Sourcepub fn try_acquire(&mut self, tokens: f64) -> bool
pub fn try_acquire(&mut self, tokens: f64) -> bool
Try to acquire tokens
Returns true if tokens were acquired, false if rate limited
Sourcepub async fn acquire(&mut self, tokens: f64)
pub async fn acquire(&mut self, tokens: f64)
Wait until tokens are available
Blocks until the specified number of tokens are available
Sourcepub fn current_tokens(&mut self) -> f64
pub fn current_tokens(&mut self) -> f64
Get current token count
Sourcepub fn time_until_available(&mut self, tokens: f64) -> Duration
pub fn time_until_available(&mut self, tokens: f64) -> Duration
Get time until tokens are available
Auto Trait Implementations§
impl Freeze for TokenBucketLimiter
impl RefUnwindSafe for TokenBucketLimiter
impl Send for TokenBucketLimiter
impl Sync for TokenBucketLimiter
impl Unpin for TokenBucketLimiter
impl UnwindSafe for TokenBucketLimiter
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