Function dynpool::cache_scale

source ·
pub fn cache_scale(sys: impl System, rate: Duration) -> impl System
Expand description

Wrap a system so that System::scale is called no more than once per period of time, and is otherwise cached. This should be used if scale calculation is expensive.

struct Sys;

impl System for Sys {
    type Data = ();

    fn init(&self, _: usize) {}

    fn work(&self, _: &mut ()) -> Decision { Decision::Again }

    fn scale(&self) -> Scale {
        sleep_ms(100); // scale function is slow
        return Scale::active(10);
    }
}

let sys = cache_scale(Sys, Duration::from_millis(500));
Pool::start_fg(shutdown_after(sys, time));