Struct leaky_bucket_lite::LeakyBucket[][src]

pub struct LeakyBucket { /* fields omitted */ }
Expand description

The leaky bucket.

Implementations

Construct a new leaky bucket through a builder.

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

Get the current number of tokens available.

Errors

Returns an Error when communicating with the actor fails.

Acquire a single token.

This is identical to acquire with an argument of 1.0.

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.0)
        .tokens(0.0)
        .refill_interval(Duration::from_secs(5))
        .refill_amount(1.0)
        .build();

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

    Ok(())
}

Errors

Returns an Error when communicating with the actor fails.

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.0)
        .tokens(0.0)
        .refill_interval(Duration::from_secs(5))
        .refill_amount(1.0)
        .build();

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

    Ok(())
}

Errors

Returns an Error when communicating with the actor fails.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.