Struct ratelimit::Handle [] [src]

pub struct Handle { /* fields omitted */ }

Handler for a multi-threaded rate-limiter.

Methods

impl Handle
[src]

[src]

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!(".");
        }
    }));
}

[src]

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!(".");
        }
    }));
}

[src]

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");
}

[src]

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

impl Clone for Handle
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

impl Sync for Handle
[src]