pub struct TokenBucket {
pub capacity: f64,
pub refill_rate: f64,
/* private fields */
}Expand description
A token-bucket rate limiter.
Tokens accumulate at refill_rate tokens per millisecond up to capacity.
Each request consumes tokens tokens. Requests that cannot be served
immediately are rejected (non-blocking).
Fields§
§capacity: f64Maximum number of tokens the bucket can hold.
refill_rate: f64Token refill rate in tokens per millisecond.
Implementations§
Source§impl TokenBucket
impl TokenBucket
Sourcepub fn new(capacity: f64, refill_rate: f64, now_ms: u64) -> Self
pub fn new(capacity: f64, refill_rate: f64, now_ms: u64) -> Self
Create a new token bucket, starting full.
Sourcepub fn try_consume(&mut self, tokens: f64, now_ms: u64) -> bool
pub fn try_consume(&mut self, tokens: f64, now_ms: u64) -> bool
Try to consume tokens from the bucket.
Returns true if the tokens were available and consumed, false
otherwise (no tokens are consumed on failure).
Trait Implementations§
Source§impl Clone for TokenBucket
impl Clone for TokenBucket
Source§fn clone(&self) -> TokenBucket
fn clone(&self) -> TokenBucket
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · 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 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> 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