From 8ecbb56c6d6611e251dce880e24f561ecc6a2c06 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Tue, 25 Aug 2020 18:34:06 +0200 Subject: [PATCH 1/2] src/lib: Make raise_fd_limit return new limit Add return value `Option` to `raise_fd_limit` returning the new file descriptor limit to the caller. --- src/lib.rs | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 48de0fc..72f7b0f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,13 +14,23 @@ extern crate libc; +/// Raise the soft open file descriptor resource limit to the smaller of the +/// kernel limit and the hard resource limit. +/// +/// Returns [`Some`] with the new limit. +/// +/// # Panics +/// +/// Panics if [`libc::sysctl`], [`libc::getrlimit`] or [`libc::setrlimit`] +/// fail. +/// /// darwin_fd_limit exists to work around an issue where launchctl on Mac OS X /// defaults the rlimit maxfiles to 256/unlimited. The default soft limit of 256 /// ends up being far too low for our multithreaded scheduler testing, depending /// on the number of cores available. #[cfg(any(target_os = "macos", target_os = "ios"))] #[allow(non_camel_case_types)] -pub fn raise_fd_limit() { +pub fn raise_fd_limit() -> Option { use std::cmp; use std::io; use std::mem::size_of_val; @@ -60,13 +70,22 @@ pub fn raise_fd_limit() { let err = io::Error::last_os_error(); panic!("raise_fd_limit: error calling setrlimit: {}", err); } + + Some(rlim.rlim_cur) } } +/// Raise the soft open file descriptor resource limit to the hard resource +/// limit. +/// +/// Returns [`Some`] with the new limit. +/// +/// # Panics +/// +/// Panics if [`libc::getrlimit`] or [`libc::setrlimit`] fail. #[cfg(any(target_os = "linux"))] #[allow(non_camel_case_types)] -pub fn raise_fd_limit() { - use libc; +pub fn raise_fd_limit() -> Option { use std::io; unsafe { @@ -85,8 +104,13 @@ pub fn raise_fd_limit() { let err = io::Error::last_os_error(); panic!("raise_fd_limit: error calling setrlimit: {}", err); } + + Some(rlim.rlim_cur) } } +/// Returns [`None`]. #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "linux")))] -pub fn raise_fd_limit() {} +pub fn raise_fd_limit() -> Option { + None +} From e59cd0dd5a794e42979e1f377edac44983dcc57c Mon Sep 17 00:00:00 2001 From: Max Inden Date: Tue, 25 Aug 2020 19:16:23 +0200 Subject: [PATCH 2/2] Cargo.toml: Bump version to 0.2.0 --- Cargo.toml | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 73ce852..b646a52 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fdlimit" -version = "0.1.4" +version = "0.2.0" authors = ["Parity Technologies"] license = "Apache-2.0" description = "Utility crate for raising file descriptors limit for OSX and Linux" diff --git a/README.md b/README.md index fbb748b..3c3ab43 100644 --- a/README.md +++ b/README.md @@ -12,5 +12,5 @@ on Windows does nothing add in Cargo.toml: ``` [dependencies] -fdlimit = "0.1.1" +fdlimit = "0.2.0" ```