Struct ratelimit_rs::Bucket[][src]

pub struct Bucket {
    pub capacity: u64,
    pub available_tokens: u64,
    pub quantum: u64,
    pub fill_interval: Duration,
    pub latest_tick: Instant,
}

Bucket represents a token bucket that fills at a predetermined rate. Methods on Bucket may be called concurrently.

Fields

capacity: u64

capacity holds the overall capacity of the bucket.

available_tokens: u64

availableTokens holds the number of available tokens as of the associated latestTick. It will be negative when there are consumers waiting for tokens.

quantum: u64

quantum holds how many tokens are added on each tick.

fill_interval: Duration

fillInterval holds the interval between each tick.

latest_tick: Instant

latestTick holds the latest tick for which we know the number of tokens in the bucket.

Implementations

impl Bucket[src]

pub fn new(
    fill_interval: Duration,
    capacity: u64,
    quantum: u64,
    available_tokens: u64
) -> Self
[src]

pub fn take_available(&mut self, count: u64) -> u64[src]

TakeAvailable takes up to count immediately available tokens from the bucket. It returns the number of tokens removed, or zero if there are no available tokens. It does not block.

pub fn take_one_available(&mut self) -> u64[src]

TakeOneAvailable takes up a token from the bucket.

pub fn take_max_duration(
    &mut self,
    count: u64,
    max_wait: Duration
) -> (Duration, bool)
[src]

TakeMaxDuration is take, except that it will only take tokens from the bucket if the wait time for the tokens is no greater than maxWait. If it would take longer than maxWait for the tokens to become available, it does nothing and reports false, otherwise it returns the time that the caller should wait until the tokens are actually available, and reports true.

pub fn wait_max_duration(&mut self, count: u64, max_wait: Duration) -> bool[src]

WaitMaxDuration is like Wait except that it will only take tokens from the bucket if it needs to wait for no greater than maxWait. It reports whether any tokens have been removed from the bucket If no tokens have been removed, it returns immediately.

Trait Implementations

impl Debug for Bucket[src]

Auto Trait Implementations

impl RefUnwindSafe for Bucket

impl Send for Bucket

impl Sync for Bucket

impl Unpin for Bucket

impl UnwindSafe for Bucket

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.