Skip to main content

workflow_core/
fd.rs

1//! Module for the file descriptor limit management.
2
3///
4#[cfg(not(target_arch = "wasm32"))]
5pub fn try_set_fd_limit(limit: u64) -> std::io::Result<u64> {
6    cfg_if::cfg_if! {
7        if #[cfg(target_os = "windows")] {
8                rlimit::setmaxstdio(limit as u32).map(|v| v as u64)
9        } else if #[cfg(unix)] {
10            rlimit::increase_nofile_limit(limit)
11        }
12    }
13}
14
15/// Returns the current open file descriptor limit for the process, or an error
16/// on platforms where the limit cannot be queried.
17pub fn limit() -> std::io::Result<i32> {
18    cfg_if::cfg_if! {
19        if #[cfg(target_os = "windows")] {
20            Ok(rlimit::getmaxstdio() as i32)
21        }
22        else if #[cfg(unix)] {
23            Ok(rlimit::getrlimit(rlimit::Resource::NOFILE).unwrap().0 as i32)
24        }
25        else {
26            Err(std::io::Error::new(std::io::ErrorKind::Other, "Unsupported platform: file descriptor limit is not available"))
27        }
28    }
29}