Struct ratelimit::Ratelimiter

source ·
pub struct Ratelimiter { /* private fields */ }

Implementations§

source§

impl Ratelimiter

source

pub fn builder(amount: u64, interval: Duration) -> Builder

Initialize a builder that will construct a Ratelimiter that adds the specified amount of tokens to the token bucket after each interval has elapsed.

Note: In practice, the system clock resolution imposes a lower bound on the interval. To be safe, it is recommended to set the interval to be no less than 1 microsecond. This also means that the number of tokens per interval should be > 1 to achieve rates beyond 1 million tokens/s.

source

pub fn rate(&self) -> f64

Return the current effective rate of the Ratelimiter in tokens/second

source

pub fn refill_interval(&self) -> Duration

Return the current interval between refills.

source

pub fn set_refill_interval(&self, duration: Duration) -> Result<(), Error>

Allows for changing the interval between refills at runtime.

source

pub fn refill_amount(&self) -> u64

Return the current number of tokens to be added on each refill.

source

pub fn set_refill_amount(&self, amount: u64) -> Result<(), Error>

Allows for changing the number of tokens to be added on each refill.

source

pub fn max_tokens(&self) -> u64

Returns the maximum number of tokens that can

source

pub fn set_max_tokens(&self, amount: u64) -> Result<(), Error>

Allows for changing the maximum number of tokens that can be held by the ratelimiter for immediate use. This effectively sets the burst size. The configured value must be greater than or equal to the refill amount.

source

pub fn available(&self) -> u64

Returns the number of tokens currently available.

source

pub fn set_available(&self, amount: u64) -> Result<(), Error>

Sets the number of tokens available to some amount. Returns an error if the amount exceeds the bucket capacity.

source

pub fn dropped(&self) -> u64

Returns the number of tokens that have been dropped due to bucket overflowing.

source

pub fn try_wait(&self) -> Result<(), Duration>

Non-blocking function to “wait” for a single token. On success, a single token has been acquired. On failure, a Duration hinting at when the next refill would occur is returned.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.