From 6826af1b50e62f06f32dc1d9eda97da4c702600f Mon Sep 17 00:00:00 2001 From: Matt Stancliff Date: Sat, 22 Mar 2014 19:15:04 -0400 Subject: [PATCH] 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). --- src/redis.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/redis.c b/src/redis.c index 64c80bd9..8a482c63 100644 --- a/src/redis.c +++ b/src/redis.c @@ -1417,21 +1417,21 @@ void initServerConfig() { } /* 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 - * file descriptors as we need a few more for persistence, listening - * sockets, log files and so forth. + * the configured max number of clients. It also reserves a number of file + * descriptors (REDIS_EVENTLOOP_FDSET_INCR) for extra operations of + * persistence, listening sockets, log files and so forth. * * 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 * server.maxclients to the value that we can actually handle. */ void adjustOpenFilesLimit(void) { - rlim_t maxfiles = server.maxclients+32; + rlim_t maxfiles = server.maxclients+REDIS_EVENTLOOP_FDSET_INCR; struct rlimit limit; 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.", strerror(errno)); - server.maxclients = 1024-32; + server.maxclients = 1024-REDIS_EVENTLOOP_FDSET_INCR; } else { rlim_t oldlimit = limit.rlim_cur; @@ -1445,11 +1445,11 @@ void adjustOpenFilesLimit(void) { limit.rlim_cur = f; limit.rlim_max = f; if (setrlimit(RLIMIT_NOFILE,&limit) != -1) break; - f -= 128; + f -= REDIS_EVENTLOOP_FDSET_INCR; } if (f < oldlimit) f = oldlimit; 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.", (int) maxfiles, strerror(errno), (int) server.maxclients); } else { -- GitLab