Struct ratelimit::Limiter

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

Single-threaded rate limiter.

Implementations

create a new default ratelimit instance

Example

let mut r = Limiter::new();

create a new Config for building a custom Limiter

Example

let mut r = Limiter::configure().build();

Run the limiter, intended for multi-threaded use

Example

let mut limiter = Limiter::new();

let mut handle = limiter.make_handle();

thread::spawn(move || { limiter.run(); });

handle.wait();

Create a Handle for multi-thread use

Example

let mut limiter = Limiter::new();

let mut handle = limiter.make_handle();

match handle.try_wait() {
    Ok(_) => {
        println!("not limited");
    },
    Err(_) => {
        println!("was limited");
    },
}

Blocking wait for 1 token.

Example

let mut limiter = Limiter::new();
for i in -10..0 {
    println!("T {}...", i);
    limiter.wait();
}
println!("Ignition!");

Non-blocking wait for one token. Returns Ok(()) if no wait required. Returns Err(()) if wait would block.

Examples
let mut limiter = Limiter::new();

if limiter.try_wait().is_ok() {
    println!("token granted");
} else {
    println!("would block");
}

Blocking wait for count tokens.

Example

let mut limiter = Limiter::new();
for i in 1..3 {
    println!("{}...", i);
    limiter.wait_for(i);
}

Returns Ok(()) if no wait required. Returns Err(()) if wait would block.

Examples
let mut limiter = Limiter::new();

if limiter.try_wait_for(2).is_ok() {
    println!("tokens granted");
} else {
    println!("would block");
}

Trait Implementations

Returns the “default value” for a type. 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

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

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.