-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
nofile.rs
37 lines (31 loc) · 1.04 KB
/
nofile.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#[cfg(unix)]
mod unix_limits {
use std::cmp;
use std::io;
use rlimit::Resource;
const DEFAULT_NOFILE_LIMIT: u64 = 16384; // or another number
/// Try to increase NOFILE limit and return the current soft limit.
pub fn increase_nofile_limit() -> io::Result<u64> {
let (soft, hard) = Resource::NOFILE.get()?;
println!("Before increasing: soft = {soft}, hard = {hard}");
let target = cmp::min(DEFAULT_NOFILE_LIMIT, hard);
println!("Try to increase: target = {target}");
Resource::NOFILE.set(target, hard)?;
let (soft, hard) = Resource::NOFILE.get()?;
println!("After increasing: soft = {soft}, hard = {hard}");
Ok(soft)
}
}
fn main() {
#[cfg(unix)]
{
match unix_limits::increase_nofile_limit() {
Ok(soft) => println!("NOFILE limit: soft = {soft}"),
Err(err) => println!("Failed to increase NOFILE limit: {err}"),
}
}
#[cfg(not(unix))]
{
println!("Do nothing on non-Unix systems");
}
}