pub struct Limiter { /* private fields */ }
Expand description
Single-threaded rate limiter.
Implementations
sourceimpl Limiter
impl Limiter
sourcepub fn run(&mut self)
pub fn run(&mut self)
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();
sourcepub fn make_handle(&mut self) -> Handle
pub fn make_handle(&mut self) -> Handle
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");
},
}
sourcepub fn wait(&mut self)
pub fn wait(&mut self)
Blocking wait for 1 token.
Example
let mut limiter = Limiter::new();
for i in -10..0 {
println!("T {}...", i);
limiter.wait();
}
println!("Ignition!");
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 = Limiter::new();
if limiter.try_wait().is_ok() {
println!("token granted");
} else {
println!("would block");
}
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Limiter
impl Send for Limiter
impl !Sync for Limiter
impl Unpin for Limiter
impl UnwindSafe for Limiter
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