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