From d28eef9091e7570d3fb20990e2b1733a1692338f Mon Sep 17 00:00:00 2001 From: Markus Ongyerth Date: Wed, 9 Oct 2024 07:56:53 +0000 Subject: [PATCH 1/5] Allow users to increase the fileno limit This allows wstunnel to use fds>1024. Otherwise heavy use can easily run out of file descriptors on connection attempts. While there will still be a limit, it is significantly higher (~500 times on my system) which provides enough headroom for connections to be torn down and fds to be closed. --- Cargo.lock | 11 +++++++++++ Cargo.toml | 2 ++ src/main.rs | 17 +++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 120582a..c7d366a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -759,6 +759,16 @@ dependencies = [ "utf-8", ] +[[package]] +name = "fdlimit" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e182f7dbc2ef73d9ef67351c5fbbea084729c48362d3ce9dd44c28e32e277fe5" +dependencies = [ + "libc", + "thiserror", +] + [[package]] name = "filetime" version = "0.2.25" @@ -3130,6 +3140,7 @@ dependencies = [ "crossterm", "fast-socks5", "fastwebsockets", + "fdlimit", "futures-util", "hickory-resolver", "http-body-util", diff --git a/Cargo.toml b/Cargo.toml index 7bb9e16..6d4cc43 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,8 @@ hickory-resolver = { version = "0.24.1", features = ["tokio", "dns-over-https-ru ppp = { version = "2.2.0", features = [] } async-channel = { version = "2.3.1", features = [] } +fdlimit = "0.3.0" + # For config file parsing regex = { version = "1.11.0", default-features = false, features = ["std", "perf"] } serde_regex = "1.1.0" diff --git a/src/main.rs b/src/main.rs index 3f3ecc6..27f1fb0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -74,6 +74,19 @@ struct Wstunnel { default_value = "INFO" )] log_lvl: String, + + /// Increae the fileno soft limit to the fileno hard limit + /// The soft limit defaults to 1024 on linux for legacy reasons and is not enough for heavy use + /// of wstunnel proxy features. + /// The hard limit is significantly higher and will usually suffice. + #[arg( + long, + global = true, + value_name = "HARD_FILENO", + verbatim_doc_comment, + default_value = None + )] + hard_fileno: Option, } #[derive(clap::Subcommand, Debug)] @@ -728,6 +741,10 @@ async fn main() -> anyhow::Result<()> { logger.init(); }; + if args.hard_fileno.is_some() { + fdlimit::raise_fd_limit()?; + } + match args.commands { Commands::Client(args) => { let (tls_certificate, tls_key) = if let (Some(cert), Some(key)) = From ada5f1d66475b6d502d88e11b671cd36d5e183af Mon Sep 17 00:00:00 2001 From: Markus Ongyerth Date: Wed, 9 Oct 2024 08:57:19 +0000 Subject: [PATCH 2/5] Use proper bool for hard-fileno --- src/main.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 27f1fb0..659b4ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -82,11 +82,10 @@ struct Wstunnel { #[arg( long, global = true, - value_name = "HARD_FILENO", verbatim_doc_comment, - default_value = None + default_value = "false" )] - hard_fileno: Option, + hard_fileno: bool, } #[derive(clap::Subcommand, Debug)] @@ -741,7 +740,7 @@ async fn main() -> anyhow::Result<()> { logger.init(); }; - if args.hard_fileno.is_some() { + if args.hard_fileno { fdlimit::raise_fd_limit()?; } From 7b316a5ad25e55ab44f569cf0963cd7d21551cd9 Mon Sep 17 00:00:00 2001 From: Markus Ongyerth Date: Wed, 9 Oct 2024 09:00:30 +0000 Subject: [PATCH 3/5] Follow lint formatting --- src/main.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 659b4ad..48e5445 100644 --- a/src/main.rs +++ b/src/main.rs @@ -79,12 +79,7 @@ struct Wstunnel { /// The soft limit defaults to 1024 on linux for legacy reasons and is not enough for heavy use /// of wstunnel proxy features. /// The hard limit is significantly higher and will usually suffice. - #[arg( - long, - global = true, - verbatim_doc_comment, - default_value = "false" - )] + #[arg(long, global = true, verbatim_doc_comment, default_value = "false")] hard_fileno: bool, } From 659f05fe79030b6e4ee3fbada15d6dcd9e323cb3 Mon Sep 17 00:00:00 2001 From: Markus Ongyerth Date: Thu, 7 Nov 2024 11:26:14 +0100 Subject: [PATCH 4/5] Use hard fileno by default --- src/main.rs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 48e5445..8377756 100644 --- a/src/main.rs +++ b/src/main.rs @@ -74,13 +74,6 @@ struct Wstunnel { default_value = "INFO" )] log_lvl: String, - - /// Increae the fileno soft limit to the fileno hard limit - /// The soft limit defaults to 1024 on linux for legacy reasons and is not enough for heavy use - /// of wstunnel proxy features. - /// The hard limit is significantly higher and will usually suffice. - #[arg(long, global = true, verbatim_doc_comment, default_value = "false")] - hard_fileno: bool, } #[derive(clap::Subcommand, Debug)] @@ -734,10 +727,7 @@ async fn main() -> anyhow::Result<()> { } else { logger.init(); }; - - if args.hard_fileno { - fdlimit::raise_fd_limit()?; - } + fdlimit::raise_fd_limit()?; match args.commands { Commands::Client(args) => { From a53b9dd71b61babd9eb698e518a7f0e2082ee061 Mon Sep 17 00:00:00 2001 From: Markus Ongyerth Date: Thu, 7 Nov 2024 11:29:08 +0100 Subject: [PATCH 5/5] Only log errors from increasing file limit --- src/main.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8377756..1012ee0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,7 @@ use base64::Engine; use clap::Parser; use hyper::header::HOST; use hyper::http::{HeaderName, HeaderValue}; -use log::debug; +use log::{debug, warn}; use parking_lot::{Mutex, RwLock}; use std::collections::BTreeMap; use std::fmt::Debug; @@ -727,7 +727,9 @@ async fn main() -> anyhow::Result<()> { } else { logger.init(); }; - fdlimit::raise_fd_limit()?; + if let Err(err) = fdlimit::raise_fd_limit() { + warn!("Failed to set soft filelimit to hard file limit: {}", err) + } match args.commands { Commands::Client(args) => {