From b564e7ca59818e4904fc421fc8b1914cefd79538 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 5 Sep 2019 15:09:13 -0400 Subject: [PATCH] os: raise maximum file descriptor limit Do a binary search for the maximum RLIMIT_NOFILE. Patch lifted from node.js commit 6820054d2d42ff9274ea0755bea59cfc4f26f353 --- src/os.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/os.cpp b/src/os.cpp index 5fa70bd260..3a6ed2c286 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -45,6 +45,7 @@ typedef SSIZE_T ssize_t; #include #include #include +#include #include #include #include @@ -1374,6 +1375,29 @@ int os_init(void) { #elif defined(__MACH__) host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &macos_monotonic_clock); host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &macos_calendar_clock); +#endif +#if defined(ZIG_OS_POSIX) + // Raise the open file descriptor limit. + // Code lifted from node.js + struct rlimit lim; + if (getrlimit(RLIMIT_NOFILE, &lim) == 0 && lim.rlim_cur != lim.rlim_max) { + // Do a binary search for the limit. + rlim_t min = lim.rlim_cur; + rlim_t max = 1 << 20; + // But if there's a defined upper bound, don't search, just set it. + if (lim.rlim_max != RLIM_INFINITY) { + min = lim.rlim_max; + max = lim.rlim_max; + } + do { + lim.rlim_cur = min + (max - min) / 2; + if (setrlimit(RLIMIT_NOFILE, &lim)) { + max = lim.rlim_cur; + } else { + min = lim.rlim_cur; + } + } while (min + 1 < max); + } #endif return 0; }