pub struct Handle { /* private fields */ }
Expand description
Handler for a multi-threaded rate-limiter.
Implementations
sourceimpl Handle
impl Handle
sourcepub fn wait(&mut self)
pub fn wait(&mut self)
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!(".");
}
}));
}
sourcepub fn wait_for(&mut self, count: usize)
pub fn wait_for(&mut self, count: usize)
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!(".");
}
}));
}
sourcepub fn try_wait(&mut self) -> Result<(), ()>
pub fn try_wait(&mut self) -> Result<(), ()>
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");
}
sourcepub fn try_wait_for(&mut self, count: usize) -> Result<(), ()>
pub fn try_wait_for(&mut self, count: usize) -> Result<(), ()>
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
Auto Trait Implementations
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more