Crate mysteriouspants_throttle[−][src]
A simple throttle, used for slowing down repeated code. Use this to avoid drowning out
downstream systems. For example, if I were reading the contents of a file repeatedly (polling
for data, perhaps), or calling an external network resource, I could use a Throttle
to slow
that down to avoid resource contention or browning out a downstream service. Another potential
use of a Throttle
is in video game code to lock a framerate lower to promote predictable
gameplay or to avoid burning up user's graphics hardware unnecessarily.
This ranges in utility from a simple TPS throttle, "never go faster than x transactions per second,"
// create a new Throttle that rate limits to 10 TPS let throttle = Throttle::new_tps_throttle(10.0); let iteration_start = Instant::now(); // iterate eleven times, which at 10 TPS should take just over 1 second for _i in 0..11 { throttle.acquire(()); // do the needful } // prove that it did, in fact, take 1 second assert_eq!(iteration_start.elapsed().as_secs() == 1, true);
To more complicated variable-rate throttles, which may be as advanced as to slow in response to backpressure.
let throttle = Throttle::new_variable_throttle( |arg: u64, _| Duration::from_millis(arg)); let iteration_start = Instant::now(); for i in 0..5 { throttle.acquire(i * 100); } assert_eq!(iteration_start.elapsed().as_secs() == 1, true);
Structs
Throttle |