Integrate nix::errno into Error.

Split sys/limits.rs; float dep:nix version.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk
2026-02-25 22:10:53 +00:00
parent a910dc2777
commit 2d1a76a169
5 changed files with 43 additions and 26 deletions

View File

@@ -1,32 +1,11 @@
pub mod compute;
pub mod limits;
pub mod storage;
use std::path::PathBuf;
pub use compute::available_parallelism;
use crate::{Result, at, debug};
/// This is needed for opening lots of file descriptors, which tends to
/// happen more often when using RocksDB and making lots of federation
/// connections at startup. The soft limit is usually 1024, and the hard
/// limit is usually 512000; I've personally seen it hit >2000.
///
/// * <https://www.freedesktop.org/software/systemd/man/systemd.exec.html#id-1.12.2.1.17.6>
/// * <https://github.com/systemd/systemd/commit/0abf94923b4a95a7d89bc526efc84e7ca2b71741>
#[cfg(unix)]
pub fn maximize_fd_limit() -> Result<(), nix::errno::Errno> {
use nix::sys::resource::{Resource::RLIMIT_NOFILE as NOFILE, getrlimit, setrlimit};
let (soft_limit, hard_limit) = getrlimit(NOFILE)?;
if soft_limit < hard_limit {
setrlimit(NOFILE, hard_limit, hard_limit)?;
assert_eq!((hard_limit, hard_limit), getrlimit(NOFILE)?, "getrlimit != setrlimit");
debug!(to = hard_limit, from = soft_limit, "Raised RLIMIT_NOFILE",);
}
Ok(())
}
pub use self::{compute::available_parallelism, limits::*};
use crate::{Result, at};
/// Return a possibly corrected std::env::current_exe() even if the path is
/// marked deleted.

View File

@@ -0,0 +1,36 @@
#[cfg(unix)]
use nix::sys::resource::{Resource, getrlimit};
use crate::{Result, debug};
/// This is needed for opening lots of file descriptors, which tends to
/// happen more often when using RocksDB and making lots of federation
/// connections at startup. The soft limit is usually 1024, and the hard
/// limit is usually 512000; I've personally seen it hit >2000.
///
/// * <https://www.freedesktop.org/software/systemd/man/systemd.exec.html#id-1.12.2.1.17.6>
/// * <https://github.com/systemd/systemd/commit/0abf94923b4a95a7d89bc526efc84e7ca2b71741>
#[cfg(unix)]
pub fn maximize_fd_limit() -> Result {
use nix::sys::resource::setrlimit;
let (soft_limit, hard_limit) = max_file_descriptors()?;
if soft_limit < hard_limit {
setrlimit(Resource::RLIMIT_NOFILE, hard_limit, hard_limit)?;
assert_eq!((hard_limit, hard_limit), max_file_descriptors()?, "getrlimit != setrlimit");
debug!(to = hard_limit, from = soft_limit, "Raised RLIMIT_NOFILE");
}
Ok(())
}
#[cfg(not(unix))]
pub fn maximize_fd_limit() -> Result { Ok(()) }
#[cfg(unix)]
pub fn max_file_descriptors() -> Result<(u64, u64)> {
getrlimit(Resource::RLIMIT_NOFILE).map_err(Into::into)
}
#[cfg(not(unix))]
pub fn max_file_descriptors() -> Result<(u64, u64)> { Ok((u64::MAX, u64::MAX)) }