diff --git a/Cargo.toml b/Cargo.toml index cd76162c..af9da286 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -274,7 +274,7 @@ version = "0.6" features = ["std"] [workspace.dependencies.nix] -version = "0.30" +version = "0" default-features = false features = [ "resource", diff --git a/src/core/error/mod.rs b/src/core/error/mod.rs index 4314cab9..ab4b2b33 100644 --- a/src/core/error/mod.rs +++ b/src/core/error/mod.rs @@ -52,6 +52,9 @@ pub enum Error { CargoToml(#[from] cargo_toml::Error), #[error(transparent)] Clap(#[from] clap::error::Error), + #[cfg(unix)] + #[error(transparent)] + Errno(#[from] nix::errno::Errno), #[error(transparent)] Extension(#[from] axum::extract::rejection::ExtensionRejection), #[error(transparent)] diff --git a/src/core/utils/sys.rs b/src/core/utils/sys.rs index 541ee810..66f224c8 100644 --- a/src/core/utils/sys.rs +++ b/src/core/utils/sys.rs @@ -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. -/// -/// * -/// * -#[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. diff --git a/src/core/utils/sys/limits.rs b/src/core/utils/sys/limits.rs new file mode 100644 index 00000000..1dfee5fa --- /dev/null +++ b/src/core/utils/sys/limits.rs @@ -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. +/// +/// * +/// * +#[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)) } diff --git a/src/main/server.rs b/src/main/server.rs index b8717c22..e4833664 100644 --- a/src/main/server.rs +++ b/src/main/server.rs @@ -51,7 +51,6 @@ pub fn new(args: Option<&Args>, runtime: Option<&runtime::Handle>) -> Result