pub struct TokenBucket { /* private fields */ }Expand description
Refilling token bucket.
Tokens are stored as a signed 64-bit integer (scaled by 1000 to allow
sub-token precision) and refilled lazily on every call to
TokenBucket::try_consume.
Implementations§
Source§impl TokenBucket
impl TokenBucket
Sourcepub fn new(capacity_tokens: u64, tokens_per_minute: u64) -> Self
pub fn new(capacity_tokens: u64, tokens_per_minute: u64) -> Self
Create a new bucket with the given capacity (in whole tokens) and a per-minute refill rate. The bucket starts full.
Sourcepub fn try_consume(&self, n: u64) -> bool
pub fn try_consume(&self, n: u64) -> bool
Attempt to consume n tokens.
Refills the bucket based on elapsed time first, then deducts n.
Returns true if the tokens were available and consumed, false
otherwise (bucket remains unchanged on failure).
Sourcepub fn retry_after_ms(&self, n: u64) -> u64
pub fn retry_after_ms(&self, n: u64) -> u64
Estimated milliseconds until n tokens are available.
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request