Skip to content

Commit 6820054

Browse files
committed
src: raise maximum file descriptor limit
Do a binary search for the maximum RLIMIT_NOFILE. Works around the low, low limits on certain high, high-priced devices from Cupertino, CA.
1 parent f311963 commit 6820054

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/node.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#define umask _umask
7373
typedef int mode_t;
7474
#else
75+
#include <sys/resource.h> // getrlimit, setrlimit
7576
#include <unistd.h> // setuid, getuid
7677
#endif
7778

@@ -3103,6 +3104,28 @@ void Init(int* argc,
31033104
node_isolate = Isolate::GetCurrent();
31043105

31053106
#ifdef __POSIX__
3107+
// Raise the open file descriptor limit.
3108+
{
3109+
struct rlimit lim;
3110+
if (getrlimit(RLIMIT_NOFILE, &lim) == 0 && lim.rlim_cur != lim.rlim_max) {
3111+
// Do a binary search for the limit.
3112+
rlim_t min = lim.rlim_cur;
3113+
rlim_t max = 1 << 20;
3114+
// But if there's a defined upper bound, don't search, just set it.
3115+
if (lim.rlim_max != RLIM_INFINITY) {
3116+
min = lim.rlim_max;
3117+
max = lim.rlim_max;
3118+
}
3119+
do {
3120+
lim.rlim_cur = min + (max - min) / 2;
3121+
if (setrlimit(RLIMIT_NOFILE, &lim)) {
3122+
max = lim.rlim_cur;
3123+
} else {
3124+
min = lim.rlim_cur;
3125+
}
3126+
} while (min + 1 < max);
3127+
}
3128+
}
31063129
// Ignore SIGPIPE
31073130
RegisterSignalHandler(SIGPIPE, SIG_IGN);
31083131
RegisterSignalHandler(SIGINT, SignalExit);

0 commit comments

Comments
 (0)