From: antirez Date: Mon, 31 Oct 2011 09:48:41 +0000 (+0100) Subject: maxclients configuration is now implemented dealing with the actual process rlimits... X-Git-Url: https://git.saurik.com/redis.git/commitdiff_plain/58732c23d53d6fc652668a516556ea182b0ed0f8 maxclients configuration is now implemented dealing with the actual process rlimits. Setting maxclients to 0 no longer makes sense and is now invalid, the new default is 10000. See issue #162 for more information. --- diff --git a/redis.conf b/redis.conf index 71d6f70b..1138f2fb 100644 --- a/redis.conf +++ b/redis.conf @@ -168,13 +168,16 @@ slave-serve-stale-data yes ################################### LIMITS #################################### -# Set the max number of connected clients at the same time. By default there -# is no limit, and it's up to the number of file descriptors the Redis process -# is able to open. The special value '0' means no limits. +# Set the max number of connected clients at the same time. By default +# this limit is set to 10000 clients, however if the Redis server is not +# able ot configure the process file limit to allow for the specified limit +# the max number of allowed clients is set to the current file limit +# minus 32 (as Redis reserves a few file descriptors for internal uses). +# # Once the limit is reached Redis will close all the new connections sending # an error 'max number of clients reached'. # -# maxclients 128 +# maxclients 10000 # Don't use more memory than the specified amount of bytes. # When the memory limit is reached Redis will try to remove keys with an diff --git a/src/networking.c b/src/networking.c index 7ed5c6fc..862e69f4 100644 --- a/src/networking.c +++ b/src/networking.c @@ -412,7 +412,7 @@ static void acceptCommonHandler(int fd) { * connection. Note that we create the client instead to check before * for this condition, since now the socket is already set in nonblocking * mode and we can send an error for free using the Kernel I/O */ - if (server.maxclients && listLength(server.clients) > server.maxclients) { + if (listLength(server.clients) > server.maxclients) { char *err = "-ERR max number of clients reached\r\n"; /* That's a best effort error message, don't check write errors */ diff --git a/src/redis.c b/src/redis.c index a9d44fa9..53ac5350 100644 --- a/src/redis.c +++ b/src/redis.c @@ -856,7 +856,7 @@ void initServerConfig() { server.requirepass = NULL; server.rdbcompression = 1; server.activerehashing = 1; - server.maxclients = 0; + server.maxclients = REDIS_MAX_CLIENTS; server.bpop_blocked_clients = 0; server.maxmemory = 0; server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_LRU; @@ -1000,6 +1000,39 @@ void initServer() { slowlogInit(); bioInit(); srand(time(NULL)^getpid()); + + /* Try to raise the max number of open files accordingly to the + * configured max number of clients. Also account for 32 additional + * file descriptors as we need a few more for persistence, listening + * sockets, log files and so forth. */ + { + rlim_t maxfiles = server.maxclients+32; + struct rlimit limit; + + if (maxfiles < 1024) maxfiles = 1024; + 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; + } else { + rlim_t oldlimit = limit.rlim_cur; + + /* Set the max number of files if the current limit is not enough + * for our needs. */ + if (oldlimit < maxfiles) { + limit.rlim_cur = maxfiles; + limit.rlim_max = maxfiles; + if (setrlimit(RLIMIT_NOFILE,&limit) == -1) { + server.maxclients = oldlimit-32; + 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 { + redisLog(REDIS_NOTICE,"Max number of open files set to %d", + (int) maxfiles); + } + } + } + } } /* Populates the Redis Command Table starting from the hard coded list diff --git a/src/redis.h b/src/redis.h index 1a9891ec..54b9fc11 100644 --- a/src/redis.h +++ b/src/redis.h @@ -55,6 +55,7 @@ #define REDIS_AUTO_AOFREWRITE_MIN_SIZE (1024*1024) #define REDIS_SLOWLOG_LOG_SLOWER_THAN 10000 #define REDIS_SLOWLOG_MAX_LEN 64 +#define REDIS_MAX_CLIENTS 10000 /* Hash table parameters */ #define REDIS_HT_MINFILL 10 /* Minimal hash table fill 10% */