A pit full of rusty nails
at main 26 lines 900 B view raw
1use core::pin::Pin; 2use std::time::Duration; 3 4use nailconfig::NailConfig; 5use nailrng::FastRng; 6use rand::RngExt; 7use tokio::time::{Sleep, sleep}; 8 9use crate::boxed_future_within; 10 11/// Apply a delay/sleep based from provided configuration. Either it provides a set 12/// minimum delay, or a randomised value sampled from a range of the minimum and maximum 13/// configured delays. If the delay value is `0`, this future will poll ready immediately 14/// and no sleep will be invoked. 15pub fn delay_output(config: &NailConfig) -> Option<Pin<Box<Sleep>>> { 16 let delay = match (config.generator.min_delay, config.generator.max_delay) { 17 (min_delay, None) => min_delay, 18 (min_delay, Some(max_delay)) => FastRng::default().random_range(min_delay..=max_delay), 19 }; 20 21 if delay > 0 { 22 Some(boxed_future_within(|| sleep(Duration::from_millis(delay)))) 23 } else { 24 None 25 } 26}