Struct ratelimit::Ratelimiter
source · pub struct Ratelimiter { /* private fields */ }
Implementations§
source§impl Ratelimiter
impl Ratelimiter
sourcepub fn builder(amount: u64, interval: Duration) -> Builder
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.
sourcepub fn rate(&self) -> f64
pub fn rate(&self) -> f64
Return the current effective rate of the Ratelimiter in tokens/second
sourcepub fn refill_interval(&self) -> Duration
pub fn refill_interval(&self) -> Duration
Return the current interval between refills.
sourcepub fn set_refill_interval(&self, duration: Duration) -> Result<(), Error>
pub fn set_refill_interval(&self, duration: Duration) -> Result<(), Error>
Allows for changing the interval between refills at runtime.
sourcepub fn refill_amount(&self) -> u64
pub fn refill_amount(&self) -> u64
Return the current number of tokens to be added on each refill.
sourcepub fn set_refill_amount(&self, amount: u64) -> Result<(), Error>
pub fn set_refill_amount(&self, amount: u64) -> Result<(), Error>
Allows for changing the number of tokens to be added on each refill.
sourcepub fn max_tokens(&self) -> u64
pub fn max_tokens(&self) -> u64
Returns the maximum number of tokens that can
sourcepub fn set_max_tokens(&self, amount: u64) -> Result<(), Error>
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.
sourcepub fn set_available(&self, amount: u64) -> Result<(), Error>
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.