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: {sleep_duration:?} ten_milliseconds: {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 {tokens_per_duration} request(s) every {sleep_duration:?}");
let mut throttle_drift = tokio::time::Instant::now();
loop {
debug!("[throttle]: removing {tokens_per_duration} token(s) from channel");
throttle_drift = util::sleep_minus_drift(sleep_duration, throttle_drift).await;
if parent_receiver.try_recv().is_ok() {
info!("[throttle]: 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!("[throttle]: empty channel, exit after removing {token} tokens");
break;
}
}
}
}