forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): raise file descriptor limit on startup (denoland#10162)
Raise the soft limit to the hard limit when possible. This is similar to what Node.js does to avoid running into "out of file descriptors" errors too quickly. On most Linux systems, raises the limit from 1,024 to 1,048,576. On most macOS systems, raises the limit from 256 to 10,240. Fixes denoland#10148.
- Loading branch information
1 parent
df49a84
commit d46b37f
Showing
3 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/// Raise soft file descriptor limit to hard file descriptor limit. | ||
/// This is the difference between `ulimit -n` and `ulimit -n -H`. | ||
pub fn raise_fd_limit() { | ||
#[cfg(unix)] | ||
unsafe { | ||
let mut limits = libc::rlimit { | ||
rlim_cur: 0, | ||
rlim_max: 0, | ||
}; | ||
|
||
if 0 != libc::getrlimit(libc::RLIMIT_NOFILE, &mut limits) { | ||
return; | ||
} | ||
|
||
if limits.rlim_cur == libc::RLIM_INFINITY { | ||
return; | ||
} | ||
|
||
// No hard limit? Do a binary search for the effective soft limit. | ||
if limits.rlim_max == libc::RLIM_INFINITY { | ||
let mut min = limits.rlim_cur; | ||
let mut max = 1 << 20; | ||
|
||
while min + 1 < max { | ||
limits.rlim_cur = min + (max - min) / 2; | ||
match libc::setrlimit(libc::RLIMIT_NOFILE, &limits) { | ||
0 => min = limits.rlim_cur, | ||
_ => max = limits.rlim_cur, | ||
} | ||
} | ||
|
||
return; | ||
} | ||
|
||
// Raise the soft limit to the hard limit. | ||
if limits.rlim_cur < limits.rlim_max { | ||
limits.rlim_cur = limits.rlim_max; | ||
libc::setrlimit(libc::RLIMIT_NOFILE, &limits); | ||
} | ||
} | ||
} |