From 5be1e20eb4c0b6e7a9286bed606711b0f73f46f0 Mon Sep 17 00:00:00 2001 From: Charles Hall Date: Mon, 21 Oct 2024 11:25:08 -0700 Subject: [PATCH] call maximize_fd_limit at top of main This way we don't shoot ourselves in the foot by forgetting to do it for other subcommands (e.g. that manipulate the database) in the future. --- src/cli/serve.rs | 29 ----------------------------- src/main.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/cli/serve.rs b/src/cli/serve.rs index 69d5eb2b..310c97a2 100644 --- a/src/cli/serve.rs +++ b/src/cli/serve.rs @@ -78,17 +78,6 @@ pub(crate) async fn run(args: ServeArgs) -> Result<(), error::ServeCommand> { let (_guard, reload_handles) = observability::init(&config)?; - // 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)] - maximize_fd_limit() - .expect("should be able to increase the soft limit to the hard limit"); - info!("Loading database"); let db = Box::leak(Box::new( KeyValueDatabase::load_or_create(&config) @@ -908,21 +897,3 @@ fn method_to_filter(method: Method) -> MethodFilter { m => panic!("Unsupported HTTP method: {m:?}"), } } - -#[cfg(unix)] -#[tracing::instrument(err)] -fn maximize_fd_limit() -> Result<(), nix::errno::Errno> { - use nix::sys::resource::{getrlimit, setrlimit, Resource}; - - let res = Resource::RLIMIT_NOFILE; - - let (soft_limit, hard_limit) = getrlimit(res)?; - - debug!(soft_limit, "Current nofile soft limit"); - - setrlimit(res, hard_limit, hard_limit)?; - - debug!(hard_limit, "Increased nofile soft limit to the hard limit"); - - Ok(()) -} diff --git a/src/main.rs b/src/main.rs index 2726b4bd..8f4268a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -84,6 +84,17 @@ fn set_application_state(state: ApplicationState) { #[tokio::main] async fn main() -> ExitCode { + // 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)] + maximize_fd_limit() + .expect("should be able to increase the soft limit to the hard limit"); + let args = cli::Args::parse(); let Err(e) = args.run().await else { return ExitCode::SUCCESS; @@ -99,3 +110,22 @@ async fn main() -> ExitCode { ExitCode::FAILURE } + +#[cfg(unix)] +#[tracing::instrument(err)] +fn maximize_fd_limit() -> Result<(), nix::errno::Errno> { + use nix::sys::resource::{getrlimit, setrlimit, Resource}; + use tracing::debug; + + let res = Resource::RLIMIT_NOFILE; + + let (soft_limit, hard_limit) = getrlimit(res)?; + + debug!(soft_limit, "Current nofile soft limit"); + + setrlimit(res, hard_limit, hard_limit)?; + + debug!(hard_limit, "Increased nofile soft limit to the hard limit"); + + Ok(()) +}