Struct ratelimit::Handle

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

Handler for a multi-threaded rate-limiter.

Implementations

Block for 1 token

Examples
let mut limiter = Builder::default().build();
let mut handle = limiter.make_handle();

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

let mut threads = Vec::new();

for i in 0..2 {
    let mut handle = handle.clone();
    threads.push(thread::spawn(move || {
        for x in 0..5 {
            handle.wait();
            println!(".");
        }
    }));
}

Block for count tokens

Examples
let mut limiter = Builder::default().build();
let mut handle = limiter.make_handle();

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

let mut threads = Vec::new();

for i in 0..2 {
    let mut handle = handle.clone();
    threads.push(thread::spawn(move || {
        for x in 0..5 {
            handle.wait_for(1);
            println!(".");
        }
    }));
}

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

Examples
let mut limiter = Builder::new().build();
let mut handle = limiter.make_handle();

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

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

Examples
let mut limiter = Builder::new().build();
let mut handle = limiter.make_handle();

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

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. 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 resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
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.