use std::future::Future;
use std::sync::{Arc, OnceLock};
use std::time::Duration;
use tokio::sync::Semaphore;
pub(crate) const MAX_BACKGROUND: usize = 64;
pub(crate) const BACKGROUND_TIMEOUT: Duration = Duration::from_secs(30);
#[derive(Clone)]
pub(crate) struct BackgroundGate {
permits: Arc<Semaphore>,
timeout: Duration,
}
impl BackgroundGate {
pub(crate) fn new(max_in_flight: usize, timeout: Duration) -> Self {
Self {
permits: Arc::new(Semaphore::new(max_in_flight)),
timeout,
}
}
pub(crate) fn try_spawn<F>(&self, what: &'static str, fut: F) -> bool
where
F: Future<Output = ()> + Send + 'static,
{
let Ok(permit) = self.permits.clone().try_acquire_owned() else {
tracing::warn!(what, "background work at its in-flight cap; dropping it");
return false;
};
let timeout = self.timeout;
tokio::spawn(async move {
let _slot = permit;
if tokio::time::timeout(timeout, fut).await.is_err() {
tracing::warn!(what, ?timeout, "background work timed out; abandoned");
}
});
true
}
}
pub(crate) fn long_term_gate() -> &'static BackgroundGate {
static GATE: OnceLock<BackgroundGate> = OnceLock::new();
GATE.get_or_init(|| BackgroundGate::new(MAX_BACKGROUND, BACKGROUND_TIMEOUT))
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
use std::sync::atomic::{AtomicUsize, Ordering};
#[tokio::test]
async fn work_beyond_the_cap_is_dropped_not_queued() {
let gate = BackgroundGate::new(2, Duration::from_secs(30));
let release = Arc::new(tokio::sync::Notify::new());
let ran = Arc::new(AtomicUsize::new(0));
let spawn_blocked = |gate: &BackgroundGate| {
let release = release.clone();
let ran = ran.clone();
gate.try_spawn("test", async move {
ran.fetch_add(1, Ordering::SeqCst);
release.notified().await;
})
};
assert!(spawn_blocked(&gate));
assert!(spawn_blocked(&gate));
assert!(!spawn_blocked(&gate), "a third task finds no slot");
tokio::task::yield_now().await;
release.notify_waiters();
tokio::time::sleep(Duration::from_millis(20)).await;
assert_eq!(ran.load(Ordering::SeqCst), 2, "the dropped task never ran");
assert!(spawn_blocked(&gate), "slots are free again");
}
#[tokio::test(start_paused = true)]
async fn a_task_that_overruns_is_abandoned_and_frees_its_slot() {
let gate = BackgroundGate::new(1, Duration::from_secs(5));
assert!(gate.try_spawn("slow", async {
tokio::time::sleep(Duration::from_secs(3600)).await;
}));
assert!(
!gate.try_spawn("second", async {}),
"the slot is held while it runs"
);
tokio::time::sleep(Duration::from_secs(6)).await;
assert!(
gate.try_spawn("third", async {}),
"slot freed by the timeout"
);
}
}