13 unstable releases (5 breaking)

0.10.1 Jul 19, 2023
0.9.1 Jan 30, 2023
0.9.0 Dec 28, 2022
0.8.3 Apr 6, 2022
0.3.0 Jul 29, 2019

#17 in Unix APIs

Download history 35572/week @ 2023-12-14 27795/week @ 2023-12-21 25843/week @ 2023-12-28 37141/week @ 2024-01-04 40327/week @ 2024-01-11 44320/week @ 2024-01-18 47305/week @ 2024-01-25 55991/week @ 2024-02-01 38671/week @ 2024-02-08 38277/week @ 2024-02-15 47062/week @ 2024-02-22 46181/week @ 2024-02-29 46124/week @ 2024-03-07 48109/week @ 2024-03-14 44698/week @ 2024-03-21 37093/week @ 2024-03-28

184,019 downloads per month
Used in 161 crates (47 directly)

MIT license

175KB
5K SLoC

rlimit

Latest Version Documentation License Downloads

Resource limits.

Documentation: https://docs.rs/rlimit

Contributing

Sponsor

If my open-source work has been helpful to you, please sponsor me.

Every little bit helps. Thank you!


lib.rs:

rlimit - Resource limits.

Set resource limit

use rlimit::{setrlimit, Resource};

const DEFAULT_SOFT_LIMIT: u64 = 4 * 1024 * 1024;
const DEFAULT_HARD_LIMIT: u64 = 8 * 1024 * 1024;
assert!(Resource::FSIZE.set(DEFAULT_SOFT_LIMIT, DEFAULT_HARD_LIMIT).is_ok());

let soft = 16384;
let hard = soft * 2;
assert!(setrlimit(Resource::NOFILE, soft, hard).is_ok());

Get resource limit

use rlimit::{getrlimit, Resource};

assert!(Resource::NOFILE.get().is_ok());
assert_eq!(getrlimit(Resource::CPU).unwrap(), (rlimit::INFINITY, rlimit::INFINITY));

Windows

Windows does not have Unix-like resource limits. It only supports changing the number of simultaneously open files currently permitted at the stdio level.

See the official documentation of _getmaxstdio and _setmaxstdio.

println!("{}", rlimit::getmaxstdio()); // 512
rlimit::setmaxstdio(2048).unwrap();
println!("{}", rlimit::getmaxstdio()); // 2048

Increase NOFILE limit

See the example nofile.

You can also use the tool function rlimit::increase_nofile_limit

rlimit::increase_nofile_limit(10240).unwrap();
rlimit::increase_nofile_limit(u64::MAX).unwrap();

Troubleshoot

Failed to increase NOFILE to hard limit on macOS

On macOS, getrlimit by default reports that the hard limit is unlimited, but there is usually a stricter hard limit discoverable via sysctl (kern.maxfilesperproc). Failing to discover this secret stricter hard limit will cause the call to setrlimit to fail.

rlimit::increase_nofile_limit respects kern.maxfilesperproc.

Dependencies