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 2 times the refill or the initial value, whichever is largest.

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.

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 std::time;

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

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

let limiter = RateLimiter::builder()
    .interval(time::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;
use std::time;

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

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

Configure the rate limiter to be fair. By default the rate limiter is fair which ensures that all tasks make steady progress even under contention. But an unfair scheduler might have a higher total throughput.

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 std::time;

let limiter = RateLimiter::builder()
    .refill(100)
    .interval(time::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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere 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 Twhere 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 Twhere 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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more