use std::future::Future;
use std::io;
use std::sync::OnceLock;
use std::time::{Duration, Instant};
use tokio::sync::{Semaphore, SemaphorePermit};
pub(crate) type Descriptor = SemaphorePermit<'static>;
pub(crate) fn gate() -> &'static Semaphore {
static GATE: OnceLock<Semaphore> = OnceLock::new();
GATE.get_or_init(|| Semaphore::new(budget()))
}
pub(crate) fn descriptor_blocking(runtime: &tokio::runtime::Handle) -> Descriptor {
runtime
.block_on(gate().acquire())
.expect("the descriptor gate is never closed")
}
pub(crate) const PATIENCE: Duration = Duration::from_secs(10);
pub(crate) const FIRST_PAUSE: Duration = Duration::from_millis(10);
pub(crate) const LONGEST_PAUSE: Duration = Duration::from_millis(250);
pub(crate) async fn patiently<T, F, Fut>(patience: Duration, mut attempt: F) -> io::Result<T>
where
F: FnMut() -> Fut,
Fut: Future<Output = io::Result<T>>,
{
let mut refused_since: Option<Instant> = None;
let mut pause = FIRST_PAUSE;
loop {
match attempt().await {
Err(e) if exhausted(&e) => {
if refused_since.get_or_insert_with(Instant::now).elapsed() >= patience {
return Err(e);
}
tokio::time::sleep(pause).await;
pause = (pause * 2).min(LONGEST_PAUSE);
}
result => return result,
}
}
}
pub(crate) fn patiently_blocking<T>(
patience: Duration,
mut attempt: impl FnMut() -> io::Result<T>,
) -> io::Result<T> {
let mut refused_since: Option<Instant> = None;
let mut pause = FIRST_PAUSE;
loop {
match attempt() {
Err(e) if exhausted(&e) => {
if refused_since.get_or_insert_with(Instant::now).elapsed() >= patience {
return Err(e);
}
std::thread::sleep(pause);
pause = (pause * 2).min(LONGEST_PAUSE);
}
result => return result,
}
}
}
pub(crate) fn starved(patience: Duration) -> String {
starved_while(&format!("within {patience:?}"))
}
pub(crate) fn starved_while(wait: &str) -> String {
let limit = soft_limit()
.map(|limit| format!(" of {limit}"))
.unwrap_or_default();
format!("file descriptor limit{limit} reached, no socket free {wait}")
}
pub(crate) const RESERVE: usize = 16;
pub(crate) fn budget() -> usize {
budget_within(soft_limit())
}
fn budget_within(soft: Option<usize>) -> usize {
soft.map_or(Semaphore::MAX_PERMITS, |soft| {
soft.saturating_sub(reserve_within(soft))
.clamp(1, Semaphore::MAX_PERMITS)
})
}
fn reserve_within(soft: usize) -> usize {
(soft / 2).max(RESERVE)
}
pub(crate) fn too_few() -> Option<(usize, usize)> {
too_few_within(soft_limit())
}
fn too_few_within(soft: Option<usize>) -> Option<(usize, usize)> {
let needed = RESERVE + 1;
soft.filter(|&soft| soft < needed)
.map(|soft| (soft, needed))
}
pub(crate) fn soft_limit() -> Option<usize> {
#[cfg(unix)]
{
let mut bounds = libc::rlimit {
rlim_cur: 0,
rlim_max: 0,
};
if unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &mut bounds) } != 0 {
return None;
}
if bounds.rlim_cur == libc::RLIM_INFINITY {
return None;
}
usize::try_from(bounds.rlim_cur).ok()
}
#[cfg(not(unix))]
{
None
}
}
pub(crate) fn exhausted(error: &io::Error) -> bool {
let Some(code) = error.raw_os_error() else {
return false;
};
#[cfg(unix)]
{
code == libc::EMFILE || code == libc::ENFILE
}
#[cfg(windows)]
{
use windows_sys::Win32::Networking::WinSock::{WSAEMFILE, WSAENOBUFS};
code == WSAEMFILE || code == WSAENOBUFS
}
#[cfg(not(any(unix, windows)))]
{
let _ = code;
false
}
}
#[cfg(all(test, unix))]
pub(crate) mod testing {
const OWN_PROCESS: &str = "ZOND_TEST_IN_OWN_PROCESS";
pub(crate) fn in_a_process_of_its_own(module: &str, name: &str) -> bool {
if std::env::var(OWN_PROCESS).is_ok_and(|running| running == name) {
return true;
}
let path = format!(
"{}::{name}",
module.split_once("::").expect("a crate path").1
);
let run = std::process::Command::new(std::env::current_exe().expect("this test binary"))
.args([path.as_str(), "--exact", "--nocapture", "--test-threads=1"])
.env(OWN_PROCESS, name)
.output()
.expect("re-running the test in a process of its own");
let stdout = String::from_utf8_lossy(&run.stdout);
assert!(
run.status.success(),
"{name} failed in its own process:\n{stdout}{}",
String::from_utf8_lossy(&run.stderr),
);
assert!(
stdout.contains("1 passed"),
"{name} did not run in its own process:\n{stdout}"
);
false
}
pub(crate) fn exhaust(limit: libc::rlim_t) -> Vec<std::fs::File> {
let mut bounds = libc::rlimit {
rlim_cur: 0,
rlim_max: 0,
};
unsafe {
assert_eq!(libc::getrlimit(libc::RLIMIT_NOFILE, &mut bounds), 0);
bounds.rlim_cur = limit;
assert_eq!(libc::setrlimit(libc::RLIMIT_NOFILE, &bounds), 0);
}
let mut held = Vec::new();
loop {
match std::fs::File::open("/dev/null") {
Ok(file) => held.push(file),
Err(e) if e.raw_os_error() == Some(libc::EMFILE) => return held,
Err(e) => panic!("filling the descriptor table: {e}"),
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_connect_paths_take_half_of_what_the_process_may_hold() {
assert_eq!(budget_within(Some(256)), 128, "a macOS Terminal");
assert_eq!(budget_within(Some(1024)), 512, "a Linux shell");
assert_eq!(budget_within(None), Semaphore::MAX_PERMITS);
assert_eq!(budget_within(Some(usize::MAX)), Semaphore::MAX_PERMITS);
}
#[test]
fn a_small_limit_keeps_the_reserve_and_gives_the_scan_the_rest() {
assert_eq!(
budget_within(Some(48)),
24,
"half, which is above the reserve"
);
assert_eq!(budget_within(Some(24)), 24 - RESERVE);
assert_eq!(budget_within(Some(RESERVE + 1)), 1);
assert_eq!(
budget_within(Some(RESERVE)),
1,
"a gate is never empty, so a connection past the refusal still runs"
);
}
#[test]
fn a_limit_that_leaves_no_socket_beside_the_reserve_refuses_the_scan() {
assert_eq!(too_few_within(Some(16)), Some((16, RESERVE + 1)));
assert_eq!(too_few_within(Some(0)), Some((0, RESERVE + 1)));
assert_eq!(too_few_within(Some(RESERVE + 1)), None);
assert_eq!(too_few_within(Some(256)), None);
assert_eq!(too_few_within(None), None, "no limit to fall short of");
}
#[cfg(unix)]
#[tokio::test]
async fn a_full_table_is_waited_out_rather_than_passed_on() {
let mut refusals = 3;
let outcome = patiently(Duration::from_secs(10), || {
let refused = refusals > 0;
refusals -= 1;
async move {
match refused {
true => Err(io::Error::from_raw_os_error(libc::EMFILE)),
false => Ok("connected"),
}
}
})
.await;
assert_eq!(
outcome.expect("the fourth attempt had a socket"),
"connected"
);
let mut refusals = 3;
let outcome = patiently_blocking(Duration::from_secs(10), || {
refusals -= 1;
match refusals {
0.. => Err(io::Error::from_raw_os_error(libc::ENFILE)),
_ => Ok("connected"),
}
});
assert_eq!(outcome.expect("a later attempt had a socket"), "connected");
}
#[cfg(unix)]
#[tokio::test]
async fn the_wait_ends_on_its_patience_and_nothing_else_is_waited_on() {
let outcome: io::Result<()> = patiently(Duration::from_millis(30), || async {
Err(io::Error::from_raw_os_error(libc::EMFILE))
})
.await;
assert!(
outcome.is_err_and(|e| exhausted(&e)),
"the refusal is named"
);
let mut attempts = 0;
let outcome: io::Result<()> = patiently(Duration::from_secs(10), || {
attempts += 1;
async { Err(io::Error::from_raw_os_error(libc::ECONNREFUSED)) }
})
.await;
assert!(outcome.is_err_and(|e| !exhausted(&e)));
assert_eq!(attempts, 1, "a target's answer was asked again");
}
#[cfg(unix)]
#[test]
fn a_full_descriptor_table_is_told_apart_from_a_target_s_answer() {
assert!(exhausted(&io::Error::from_raw_os_error(libc::EMFILE)));
assert!(exhausted(&io::Error::from_raw_os_error(libc::ENFILE)));
assert!(!exhausted(&io::Error::from_raw_os_error(
libc::ECONNREFUSED
)));
assert!(!exhausted(&io::Error::from_raw_os_error(libc::ETIMEDOUT)));
assert!(!exhausted(&io::ErrorKind::TimedOut.into()));
}
}