[][src]Struct leaky_bucket_lite::LeakyBucket

pub struct LeakyBucket { /* fields omitted */ }

The leaky bucket.

Implementations

impl LeakyBucket[src]

pub fn builder() -> Builder[src]

Construct a new leaky bucket through a builder.

pub fn max(&self) -> usize[src]

Get the max number of tokens this rate limiter is configured for.

pub async fn acquire_one(&self) -> Result<(), Error>[src]

Acquire a single token.

This is identical to acquire with an argument of 1.

Example

use leaky_bucket_lite::LeakyBucket;
use std::{error::Error, time::Duration};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let rate_limiter = LeakyBucket::builder()
        .max(5)
        .tokens(0)
        .refill_interval(Duration::from_secs(5))
        .refill_amount(1)
        .build();

    println!("Waiting for permit...");
    // should take about 5 seconds to acquire.
    rate_limiter.acquire_one().await?;
    println!("I made it!");

    Ok(())
}

pub async fn acquire(&self, amount: usize) -> Result<(), Error>[src]

Acquire the given amount of tokens.

Example

use leaky_bucket_lite::LeakyBucket;
use std::{error::Error, time::Duration};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let rate_limiter = LeakyBucket::builder()
        .max(5)
        .tokens(0)
        .refill_interval(Duration::from_secs(5))
        .refill_amount(1)
        .build();

    println!("Waiting for permit...");
    // should take about 25 seconds to acquire.
    rate_limiter.acquire(5).await?;
    println!("I made it!");

    Ok(())
}

Trait Implementations

impl Clone for LeakyBucket[src]

impl Debug for LeakyBucket[src]

Auto Trait Implementations

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> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

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.