pub struct TokenBucket { /* private fields */ }👎Deprecated since 3.1.0: This crate has been marked for formal inclusion in the Agave Unstable API. From v4.0.0 onward, the
agave-unstable-api crate feature must be specified to acknowledge use of an interface that may break without warning.Expand description
Enforces a rate limit on the volume of requests per unit time.
Instances update the amount of tokens upon access, and thus does not need to be constantly polled to refill. Uses atomics internally so should be relatively cheap to access from many threads
Implementations§
Source§impl TokenBucket
impl TokenBucket
Sourcepub fn new(
initial_tokens: u64,
max_tokens: u64,
new_tokens_per_second: f64,
) -> Self
👎Deprecated since 3.1.0: This crate has been marked for formal inclusion in the Agave Unstable API. From v4.0.0 onward, the agave-unstable-api crate feature must be specified to acknowledge use of an interface that may break without warning.
pub fn new( initial_tokens: u64, max_tokens: u64, new_tokens_per_second: f64, ) -> Self
agave-unstable-api crate feature must be specified to acknowledge use of an interface that may break without warning.Allocate a new TokenBucket
Sourcepub fn current_tokens(&self) -> u64
👎Deprecated since 3.1.0: This crate has been marked for formal inclusion in the Agave Unstable API. From v4.0.0 onward, the agave-unstable-api crate feature must be specified to acknowledge use of an interface that may break without warning.
pub fn current_tokens(&self) -> u64
agave-unstable-api crate feature must be specified to acknowledge use of an interface that may break without warning.Return current amount of tokens in the bucket. This may be somewhat inconsistent across threads due to Relaxed atomics.
Sourcepub fn consume_tokens(&self, request_size: u64) -> Result<u64, u64>
👎Deprecated since 3.1.0: This crate has been marked for formal inclusion in the Agave Unstable API. From v4.0.0 onward, the agave-unstable-api crate feature must be specified to acknowledge use of an interface that may break without warning.
pub fn consume_tokens(&self, request_size: u64) -> Result<u64, u64>
agave-unstable-api crate feature must be specified to acknowledge use of an interface that may break without warning.Attempts to consume tokens from bucket.
On success, returns Ok(amount of tokens left in the bucket). On failure, returns Err(amount of tokens missing to fill request).
Trait Implementations§
Source§impl Clone for TokenBucket
impl Clone for TokenBucket
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Clones the TokenBucket with approximate state of the original. While this will never return an object in an invalid state, using this in a contended environment is not recommended.
1.0.0 · 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 !Freeze for TokenBucket
impl RefUnwindSafe for TokenBucket
impl Send for TokenBucket
impl Sync for TokenBucket
impl Unpin 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> 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