From 69ac8b8c4ea077fd6b0ae3d9ccb7df9e2895ea1b Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 3 Jul 2025 16:34:19 +0000 Subject: [PATCH 1/4] increasing limit for open files --- Cargo.toml | 2 +- src/main.rs | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5d4992ffd1..8a5ec78aa4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -71,7 +71,7 @@ itertools = "0.14" keyed_priority_queue = "0.4" libc = "0.2" log = "0.4" -nix = { version = "0.29", features = ["socket", "sched", "uio", "fs", "ioctl", "user", "net", "mount"] } +nix = { version = "0.29", features = ["socket", "sched", "uio", "fs", "ioctl", "user", "net", "mount", "resource" ] } once_cell = "1.21" num_cpus = "1.16" ppp = "2.3" diff --git a/src/main.rs b/src/main.rs index f11bafeec6..1695e5dfdd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,8 +14,9 @@ extern crate core; +use nix::sys::resource::{Resource, getrlimit, setrlimit}; use std::sync::Arc; -use tracing::info; +use tracing::{info, warn}; use ztunnel::*; #[cfg(feature = "jemalloc")] @@ -43,6 +44,19 @@ fn main() -> anyhow::Result<()> { } }; + if let Ok((soft_limit, hard_limit)) = getrlimit(Resource::RLIMIT_NOFILE) { + if let Err(e) = setrlimit(Resource::RLIMIT_NOFILE, hard_limit, hard_limit) { + warn!("failed to set file descriptor limits: {e}"); + } else { + info!( + "set file descriptor limits from {} to {}", + soft_limit, hard_limit + ); + } + } else { + warn!("failed to get file descriptor limits"); + } + tokio::runtime::Builder::new_current_thread() .enable_all() .build() From 1355f8732ab9a18b25097d1c693b359ca42d8aa8 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Fri, 4 Jul 2025 08:52:20 +0000 Subject: [PATCH 2/4] suggestion from PR --- src/main.rs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1695e5dfdd..cba948fc48 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,6 +29,22 @@ static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; #[unsafe(export_name = "malloc_conf")] pub static malloc_conf: &[u8] = b"prof:true,prof_active:true,lg_prof_sample:19\0"; +fn increase_open_files_limit() { + #[cfg(unix)] + if let Ok((soft_limit, hard_limit)) = getrlimit(Resource::RLIMIT_NOFILE) { + if let Err(e) = setrlimit(Resource::RLIMIT_NOFILE, hard_limit, hard_limit) { + warn!("failed to set file descriptor limits: {e}"); + } else { + info!( + "set file descriptor limits from {} to {}", + soft_limit, hard_limit + ); + } + } else { + warn!("failed to get file descriptor limits"); + } +} + fn main() -> anyhow::Result<()> { let _log_flush = telemetry::setup_logging(); @@ -44,19 +60,6 @@ fn main() -> anyhow::Result<()> { } }; - if let Ok((soft_limit, hard_limit)) = getrlimit(Resource::RLIMIT_NOFILE) { - if let Err(e) = setrlimit(Resource::RLIMIT_NOFILE, hard_limit, hard_limit) { - warn!("failed to set file descriptor limits: {e}"); - } else { - info!( - "set file descriptor limits from {} to {}", - soft_limit, hard_limit - ); - } - } else { - warn!("failed to get file descriptor limits"); - } - tokio::runtime::Builder::new_current_thread() .enable_all() .build() @@ -88,6 +91,7 @@ fn version() -> anyhow::Result<()> { async fn proxy(cfg: Arc) -> anyhow::Result<()> { info!("version: {}", version::BuildInfo::new()); + increase_open_files_limit(); info!("running with config: {}", serde_yaml::to_string(&cfg)?); app::build(cfg).await?.wait_termination().await } From da4ed5f28dde64cc727bd6c96fb2f5f7d388a363 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Fri, 4 Jul 2025 08:56:16 +0000 Subject: [PATCH 3/4] adding comment --- src/main.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main.rs b/src/main.rs index cba948fc48..15957dbc38 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,6 +29,9 @@ static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; #[unsafe(export_name = "malloc_conf")] pub static malloc_conf: &[u8] = b"prof:true,prof_active:true,lg_prof_sample:19\0"; +// We use this on Unix systems to increase the number of open file descriptors +// if possible. This is useful for high-load scenarios where the default limit +// is too low, which can lead to droopped connections and other issues (#1585). fn increase_open_files_limit() { #[cfg(unix)] if let Ok((soft_limit, hard_limit)) = getrlimit(Resource::RLIMIT_NOFILE) { From 1489886e3960926887fc34c4cdd0df2ce108487a Mon Sep 17 00:00:00 2001 From: Gustavo Meira Date: Fri, 4 Jul 2025 14:45:21 +0100 Subject: [PATCH 4/4] Update src/main.rs Co-authored-by: Daniel Hawton --- src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 15957dbc38..9b2ce84ee5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,8 @@ pub static malloc_conf: &[u8] = b"prof:true,prof_active:true,lg_prof_sample:19\0 // We use this on Unix systems to increase the number of open file descriptors // if possible. This is useful for high-load scenarios where the default limit -// is too low, which can lead to droopped connections and other issues (#1585). +// is too low, which can lead to droopped connections and other issues: +// see: https://github.com/istio/ztunnel/issues/1585 fn increase_open_files_limit() { #[cfg(unix)] if let Ok((soft_limit, hard_limit)) = getrlimit(Resource::RLIMIT_NOFILE) {