struct TokenBucket {
tokens: f64,
capacity: f64,
refill_rate: f64,
last_refill: Instant,
}Expand description
Token bucket for rate limiting
Implements the token bucket algorithm for bandwidth throttling:
- Tokens represent bytes that can be downloaded
- Tokens are replenished at a constant rate (bytes per second)
- Downloads consume tokens from the bucket
- If bucket is empty, download is throttled until tokens available
Fields§
§tokens: f64Current number of tokens available (bytes)
capacity: f64Maximum capacity of the bucket (burst allowance)
refill_rate: f64Token refill rate (bytes per second)
last_refill: InstantLast time tokens were refilled
Implementations§
Source§impl TokenBucket
impl TokenBucket
Sourcefn new(bytes_per_sec: u64, capacity_factor: f64) -> Self
fn new(bytes_per_sec: u64, capacity_factor: f64) -> Self
Create a new token bucket with specified capacity and refill rate
Sourcefn try_consume(&mut self, bytes: u64) -> u64
fn try_consume(&mut self, bytes: u64) -> u64
Try to consume the specified number of tokens (bytes) Returns number of tokens actually consumed
Trait Implementations§
Auto 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
§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>
§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Applies the layer to a service and wraps it in [
Layered].