use std::sync::Mutex;
use std::time::{Duration, Instant};
use crate::clock::Clock;
const FIRST_SPACING: Duration = Duration::from_millis(100);
const MAX_SPACING: Duration = Duration::from_secs(5);
const MIN_SPACING: Duration = Duration::from_millis(10);
#[derive(Debug, Default)]
pub struct Throttle {
state: Mutex<State>,
}
#[derive(Debug, Default)]
struct State {
spacing: Duration,
next_start: Option<Instant>,
}
impl Throttle {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn spacing(&self) -> Duration {
self.lock().spacing
}
pub(crate) async fn wait(&self, clock: &dyn Clock) {
let now = clock.now();
let start = {
let mut state = self.lock();
let start = state.next_start.map_or(now, |next| next.max(now));
state.next_start = if state.spacing.is_zero() && start == now {
None
} else {
Some(start + state.spacing)
};
start
};
if start > now {
clock.sleep(start.duration_since(now)).await;
}
}
pub(crate) fn slow_down(&self, now: Instant, delay: Duration) {
let mut state = self.lock();
let wider = state.spacing * 2;
state.spacing = wider.clamp(FIRST_SPACING, MAX_SPACING);
let resume = now + delay;
state.next_start = Some(state.next_start.map_or(resume, |next| next.max(resume)));
}
pub(crate) fn speed_up(&self) {
let mut state = self.lock();
let narrower = state.spacing.saturating_sub(state.spacing / 8);
state.spacing = if narrower < MIN_SPACING {
Duration::ZERO
} else {
narrower
};
}
fn lock(&self) -> std::sync::MutexGuard<'_, State> {
self.state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
}
#[cfg(test)]
mod tests {
use std::future::Future;
use std::pin::Pin;
use std::sync::Mutex;
use std::time::{Duration, Instant, SystemTime};
use super::Throttle;
use crate::clock::Clock;
struct Frozen {
origin: Instant,
sleeps: Mutex<Vec<Duration>>,
}
impl Frozen {
fn new() -> Self {
Self {
origin: Instant::now(),
sleeps: Mutex::new(Vec::new()),
}
}
fn take_sleeps(&self) -> Vec<Duration> {
std::mem::take(&mut *self.sleeps.lock().unwrap())
}
}
impl Clock for Frozen {
fn now(&self) -> Instant {
self.origin
}
fn wall_time(&self) -> SystemTime {
SystemTime::UNIX_EPOCH
}
fn sleep(&self, duration: Duration) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
self.sleeps.lock().unwrap().push(duration);
Box::pin(async {})
}
}
fn block_on(future: impl Future<Output = ()>) {
tokio::runtime::Builder::new_current_thread()
.build()
.unwrap()
.block_on(future);
}
#[test]
fn without_a_refusal_no_call_waits() {
let (throttle, clock) = (Throttle::new(), Frozen::new());
block_on(async {
for _ in 0..5 {
throttle.wait(&clock).await;
}
});
assert!(clock.take_sleeps().is_empty());
assert_eq!(throttle.spacing(), Duration::ZERO);
}
#[test]
fn a_refusal_holds_every_call_until_the_delay_is_over_then_spaces_them_out() {
let (throttle, clock) = (Throttle::new(), Frozen::new());
throttle.slow_down(clock.now(), Duration::from_secs(2));
block_on(async {
for _ in 0..3 {
throttle.wait(&clock).await;
}
});
let ms = Duration::from_millis;
assert_eq!(clock.take_sleeps(), [ms(2000), ms(2100), ms(2200)]);
}
#[test]
fn refusals_widen_the_gap_up_to_a_limit_and_successes_narrow_it_to_nothing() {
let (throttle, clock) = (Throttle::new(), Frozen::new());
let mut widths = Vec::new();
for _ in 0..8 {
throttle.slow_down(clock.now(), Duration::ZERO);
widths.push(throttle.spacing().as_millis());
}
let mut successes = 0;
while !throttle.spacing().is_zero() {
throttle.speed_up();
successes += 1;
}
assert_eq!(widths, [100, 200, 400, 800, 1600, 3200, 5000, 5000]);
assert_eq!(successes, 47, "about fifty successes undo the widest gap");
}
#[test]
fn a_later_refusal_never_shortens_a_pause_already_asked_for() {
let (throttle, clock) = (Throttle::new(), Frozen::new());
throttle.slow_down(clock.now(), Duration::from_secs(10));
throttle.slow_down(clock.now(), Duration::from_secs(1));
block_on(throttle.wait(&clock));
assert_eq!(clock.take_sleeps(), [Duration::from_secs(10)]);
}
}