use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
const WARNING_INTERVAL: Duration = Duration::from_secs(60);
#[derive(Clone, Debug)]
pub(crate) struct ConnLimit {
slots: Arc<Semaphore>,
max: usize,
last_warning: Arc<Mutex<Option<Instant>>>,
}
impl ConnLimit {
pub(crate) fn new(max: usize) -> Option<Self> {
(max > 0).then(|| Self {
slots: Arc::new(Semaphore::new(max)),
max,
last_warning: Arc::default(),
})
}
#[cfg(test)]
pub(crate) fn max(&self) -> usize {
self.max
}
pub(crate) async fn acquire(&self) -> OwnedSemaphorePermit {
if let Ok(permit) = self.slots.clone().try_acquire_owned() {
return permit;
}
self.warn_limit_reached();
let Ok(permit) = self.slots.clone().acquire_owned().await else {
unreachable!("the connection limit semaphore is never closed");
};
permit
}
#[cold]
fn warn_limit_reached(&self) {
let Ok(mut last_warning) = self.last_warning.lock() else {
return;
};
let now = Instant::now();
if last_warning.is_some_and(|last| now.duration_since(last) < WARNING_INTERVAL) {
return;
}
*last_warning = Some(now);
tracing::warn!(
max_connections = self.max,
"connection limit reached, new connections wait until others close"
);
}
}
#[cfg(test)]
mod tests {
use super::ConnLimit;
use std::time::Duration;
#[test]
fn conn_limit_zero_means_no_limit() {
assert!(ConnLimit::new(0).is_none());
}
#[tokio::test]
async fn conn_limit_waits_until_a_slot_is_released() {
let limit = ConnLimit::new(2).unwrap();
let first = limit.acquire().await;
let _second = limit.acquire().await;
let third = limit.acquire();
tokio::pin!(third);
assert!(
tokio::time::timeout(Duration::from_millis(100), &mut third)
.await
.is_err(),
"a third slot was granted with a limit of 2"
);
drop(first);
let _third = tokio::time::timeout(Duration::from_secs(5), third)
.await
.expect("slot not granted after one was released");
}
#[tokio::test]
async fn conn_limit_clones_share_slots() {
let limit = ConnLimit::new(1).unwrap();
let other = limit.clone();
let _permit = limit.acquire().await;
assert!(
tokio::time::timeout(Duration::from_millis(100), other.acquire())
.await
.is_err(),
"a clone granted a slot past the shared limit"
);
}
#[tokio::test]
async fn conn_limit_warns_once_per_interval() {
let (logs, _guard) = crate::server::tests::capture_debug_logs();
let limit = ConnLimit::new(1).unwrap();
let permit = limit.acquire().await;
for _ in 0..2 {
let _ = tokio::time::timeout(Duration::from_millis(50), limit.acquire()).await;
}
drop(permit);
let lines = logs.lines("connection limit reached");
assert_eq!(lines.len(), 1, "expected one warning, got: {lines:?}");
assert!(lines[0].contains("WARN"), "{}", lines[0]);
assert!(lines[0].contains("max_connections=1"), "{}", lines[0]);
}
}