提交 6826af1b 编写于 作者: M Matt Stancliff 提交者: antirez

Replace magic 32 with REDIS_EVENTLOOP_FDSET_INCR

32 was the additional number of file descriptors Redis
would reserve when managing a too-low ulimit.  The
number 32 was in too many places statically, so now
we use a macro instead that looks more appropriate.

When Redis sets up the server event loop, it uses:
    server.maxclients+REDIS_EVENTLOOP_FDSET_INCR

So, when reserving file descriptors, it makes sense to
reserve at least REDIS_EVENTLOOP_FDSET_INCR FDs instead
of only 32.  Currently, REDIS_EVENTLOOP_FDSET_INCR is
set to 128 in redis.h.

Also, I replaced the static 128 in the while f < old loop
with REDIS_EVENTLOOP_FDSET_INCR as well, which results
in no change since it was already 128.

Impact: Users now need at least maxclients+128 as
their open file limit instead of maxclients+32 to obtain
actual "maxclients" number of clients.  Redis will carve
the extra REDIS_EVENTLOOP_FDSET_INCR file descriptors it
needs out of the "maxclients" range instead of failing
to start (unless the local ulimit -n is too low to accomidate
the request).
上级 611372fa
...@@ -1417,21 +1417,21 @@ void initServerConfig() { ...@@ -1417,21 +1417,21 @@ void initServerConfig() {
} }
/* This function will try to raise the max number of open files accordingly to /* This function will try to raise the max number of open files accordingly to
* the configured max number of clients. It will also account for 32 additional * the configured max number of clients. It also reserves a number of file
* file descriptors as we need a few more for persistence, listening * descriptors (REDIS_EVENTLOOP_FDSET_INCR) for extra operations of
* sockets, log files and so forth. * persistence, listening sockets, log files and so forth.
* *
* If it will not be possible to set the limit accordingly to the configured * If it will not be possible to set the limit accordingly to the configured
* max number of clients, the function will do the reverse setting * max number of clients, the function will do the reverse setting
* server.maxclients to the value that we can actually handle. */ * server.maxclients to the value that we can actually handle. */
void adjustOpenFilesLimit(void) { void adjustOpenFilesLimit(void) {
rlim_t maxfiles = server.maxclients+32; rlim_t maxfiles = server.maxclients+REDIS_EVENTLOOP_FDSET_INCR;
struct rlimit limit; struct rlimit limit;
if (getrlimit(RLIMIT_NOFILE,&limit) == -1) { if (getrlimit(RLIMIT_NOFILE,&limit) == -1) {
redisLog(REDIS_WARNING,"Unable to obtain the current NOFILE limit (%s), assuming 1024 and setting the max clients configuration accordingly.", redisLog(REDIS_WARNING,"Unable to obtain the current NOFILE limit (%s), assuming 1024 and setting the max clients configuration accordingly.",
strerror(errno)); strerror(errno));
server.maxclients = 1024-32; server.maxclients = 1024-REDIS_EVENTLOOP_FDSET_INCR;
} else { } else {
rlim_t oldlimit = limit.rlim_cur; rlim_t oldlimit = limit.rlim_cur;
...@@ -1445,11 +1445,11 @@ void adjustOpenFilesLimit(void) { ...@@ -1445,11 +1445,11 @@ void adjustOpenFilesLimit(void) {
limit.rlim_cur = f; limit.rlim_cur = f;
limit.rlim_max = f; limit.rlim_max = f;
if (setrlimit(RLIMIT_NOFILE,&limit) != -1) break; if (setrlimit(RLIMIT_NOFILE,&limit) != -1) break;
f -= 128; f -= REDIS_EVENTLOOP_FDSET_INCR;
} }
if (f < oldlimit) f = oldlimit; if (f < oldlimit) f = oldlimit;
if (f != maxfiles) { if (f != maxfiles) {
server.maxclients = f-32; server.maxclients = f-REDIS_EVENTLOOP_FDSET_INCR;
redisLog(REDIS_WARNING,"Unable to set the max number of files limit to %d (%s), setting the max clients configuration to %d.", redisLog(REDIS_WARNING,"Unable to set the max number of files limit to %d (%s), setting the max clients configuration to %d.",
(int) maxfiles, strerror(errno), (int) server.maxclients); (int) maxfiles, strerror(errno), (int) server.maxclients);
} else { } else {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册