mysteriouspants-throttle

Crates.iomysteriouspants-throttle
lib.rsmysteriouspants-throttle
version0.2.5
sourcesrc
created_at2018-06-09 06:48:27.302633
updated_at2018-09-05 05:16:36.131176
descriptionSimple throttle implementation to make rate-limiting easy.
homepage
repositoryhttps://github.com/mysteriouspants/throttle
max_upload_size
id69282
size14,992
Chris R. Miller (mysteriouspants)

documentation

https://docs.rs/mysteriouspants-throttle

README

Throttle

Crates.io Documentation Build Status

A simple, configurable throttle for slowing down code. When do you actually want to slow down code? To avoid resource contention and browning out downstream services.

// simple throttle configured for 10 TPS
let mut throttle = Throttle::new_tps_throttle(10.0);

let iteration_start = Instant::now();

// the first one is free!
throttle.acquire(());

// the first iteration is free, subsequent iterations
// will be slowed down to a rate of 10 TPS, or one iteration
// every 100 milliseconds
for _i in 0..10 {
    throttle.acquire(());
}

println!("elapsed time: {:?}", iteration_start.elapsed());

assert_eq!(iteration_start.elapsed().as_secs() == 1, true);

Throttle is based on a functional interface, so it can go beyond constant tps rate limiting to facilitating variable-rate throttling based on conditions entirely up to your program.

let mut throttle = Throttle::new_variable_throttle(
    |iteration: u32, _| Duration::from_millis(arg));

let iteration_start = Instant::now();

// the first iteration is free, subsequent iterations
// will be slowed down
for i in 0..5 {
    throttle.acquire(i * 100);
}

assert_eq!(iteration_start.elapsed().as_secs() == 1, true);

License

I want you to be able to use this software regardless of who you may be, what you are working on, or the environment in which you are working on it - I hope you'll use it for good and not evil! To this end, Throttle is licensed under the 2-clause BSD license, with other licenses available by request. Happy coding!

Commit count: 13

cargo fmt