use tokio::time;
use crate::util;
pub async fn throttle_main(
throttle_requests: usize,
throttle_receiver: flume::Receiver<bool>,
parent_receiver: flume::Receiver<bool>,
) {
let mut sleep_duration = time::Duration::from_micros(1_000_000 / throttle_requests as u64);
let tokens_per_duration;
let ten_milliseconds = time::Duration::from_millis(10);
debug!(
"sleep_duration: {:?} ten_milliseconds: {:?}",
sleep_duration, ten_milliseconds
);
if sleep_duration < ten_milliseconds {
tokens_per_duration = (ten_milliseconds.as_nanos() / sleep_duration.as_nanos()) as u32;
sleep_duration *= tokens_per_duration;
} else {
tokens_per_duration = 1;
}
info!(
"throttle allowing {} request(s) every {:?}",
tokens_per_duration, sleep_duration
);
let mut throttle_drift = tokio::time::Instant::now();
loop {
debug!(
"throttle removing {} token(s) from channel",
tokens_per_duration
);
throttle_drift = util::sleep_minus_drift(sleep_duration, throttle_drift).await;
if parent_receiver.try_recv().is_ok() {
info!("load test complete, closing throttle channel");
drop(throttle_receiver);
break;
}
for token in 0..tokens_per_duration {
if throttle_receiver.try_recv().is_err() {
debug!("empty channel, exit after removing {} tokens", token);
break;
}
}
}
}