Struct leaky_bucket::Builder

source ·
pub struct Builder { /* private fields */ }
Expand description

A builder for a RateLimiter.

Implementations§

source§

impl Builder

source

pub fn max(&mut self, max: usize) -> &mut Self

Configure the max number of tokens to use.

If unspecified, this will default to be 10 times the refill or the initial value, whichever is largest.

The maximum supported balance is limited to isize::MAX.

§Examples
use leaky_bucket::RateLimiter;

let limiter = RateLimiter::builder()
    .max(10_000)
    .build();
source

pub fn initial(&mut self, initial: usize) -> &mut Self

Configure the initial number of tokens to configure. The default value is 0.

§Examples
use leaky_bucket::RateLimiter;

let limiter = RateLimiter::builder()
    .initial(10)
    .build();
source

pub fn interval(&mut self, interval: Duration) -> &mut Self

Configure the time duration between which we add refill number to the bucket rate limiter.

This is 100ms by default.

§Panics

This panics if the provided interval does not fit within the millisecond bounds of a usize or is zero.

use leaky_bucket::RateLimiter;
use tokio::time::Duration;

let limiter = RateLimiter::builder()
    .interval(Duration::from_secs(u64::MAX))
    .build();
use leaky_bucket::RateLimiter;
use tokio::time::Duration;

let limiter = RateLimiter::builder()
    .interval(Duration::from_millis(0))
    .build();
§Examples
use leaky_bucket::RateLimiter;
use tokio::time::Duration;

let limiter = RateLimiter::builder()
    .interval(Duration::from_millis(100))
    .build();
source

pub fn refill(&mut self, refill: usize) -> &mut Self

The number of tokens to add at each interval interval. The default value is 1.

§Panics

Panics if a refill amount of 0 is specified.

§Examples
use leaky_bucket::RateLimiter;

let limiter = RateLimiter::builder()
    .refill(100)
    .build();
source

pub fn fair(&mut self, fair: bool) -> &mut Self

Configure the rate limiter to be fair.

Fairness is enabled by deafult.

Fairness ensures that tasks make progress in the order that they acquire even when the rate limiter is under contention. An unfair scheduler might have a higher total throughput.

Fair scheduling also affects the behavior of RateLimiter::try_acquire which will return false if there are any pending tasks since they should be given priority.

§Examples
use leaky_bucket::RateLimiter;

let limiter = RateLimiter::builder()
    .refill(100)
    .fair(false)
    .build();
source

pub fn build(&self) -> RateLimiter

Construct a new RateLimiter.

§Examples
use leaky_bucket::RateLimiter;
use tokio::time::Duration;

let limiter = RateLimiter::builder()
    .refill(100)
    .interval(Duration::from_millis(200))
    .max(10_000)
    .build();

Trait Implementations§

source§

impl Default for Builder

Construct a new builder with default options.

§Examples

use leaky_bucket::Builder;

let limiter = Builder::default().build();
source§

fn default() -> Self

Returns the “default value” for a type. Read more

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.