Throttle
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 throttle = new_tps_throttle;
let iteration_start = 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
println!;
assert_eq!;
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 throttle = new_variable_throttle;
// the first one is free, so the number won't get used
throttle.acquire;
let iteration_start = now;
for i in 1..5
assert_eq!;
License
Throttle is licensed under the 2-clause BSD license.