Struct mysteriouspants_throttle::Throttle[][src]

pub struct Throttle<TArg> { /* fields omitted */ }

Methods

impl<TArg> Throttle<TArg>
[src]

Creates a new Throttle with a variable delay controlled by a closure. delay_calculator itself is an interesting type, any closure which satisfies Fn(TArg, Duration) -> Duration. It is called to determine how long the Throttle ought to wait before resuming execution, and allows you to create Throttles which respond to changes in the program or environment.

An example use of a variable-rate throttle might be to wait different periods of time depending on whether your program is in backpressure, so "ease up" on your downstream call rate, so to speak.

let throttle = Throttle::new_variable_throttle(
    |in_backpressure: bool, time_since_previous_acquire: Duration|
        match in_backpressure {
            true => Duration::from_millis(2100),
            false => Duration::from_millis(1100)
        });

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

let start_nopressure = Instant::now();
throttle.acquire(false);
assert_eq!(start_nopressure.elapsed().as_secs() == 1, true);

let start_yespressure = Instant::now();
throttle.acquire(true);
assert_eq!(start_yespressure.elapsed().as_secs() == 2, true);

Creates a new Throttle with a constant delay of tps-1 · 1000, or tps-transactions per second.

let throttle = Throttle::new_tps_throttle(0.9);

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

let start = Instant::now();
throttle.acquire(());
assert_eq!(start.elapsed().as_secs() == 1, true);

Acquires the throttle, waiting (sleeping the current thread) until enough time has passed for the running code to be at or slower than the throttle allows. The first call to acquire will never wait because there has been an undefined or arguably infinite amount of time from the previous time acquire was called. The argument arg is passed to the closure governing the wait time.

Auto Trait Implementations

impl<TArg> !Send for Throttle<TArg>

impl<TArg> !Sync for Throttle<TArg>