Skip to content
This repository
Browse code

maxclients implemented, see redis.conf for details

  • Loading branch information...
commit 285add5558c8d6cd6f6e659fbfcbc06af165218f 1 parent a0f643e
Salvatore Sanfilippo authored May 25, 2009

Showing 2 changed files with 28 additions and 1 deletion. Show diff stats Hide diff stats

  1. 19  redis.c
  2. 10  redis.conf
19  redis.c
@@ -245,6 +245,7 @@ struct redisServer {
245 245
     int masterport;
246 246
     redisClient *master;    /* client that is master for this slave */
247 247
     int replstate;
  248
+    unsigned int maxclients;
248 249
     /* Sort parameters - qsort_r() is only available under BSD so we
249 250
      * have to take this state global, in order to pass it to sortCompare() */
250 251
     int sort_desc;
@@ -862,6 +863,7 @@ static void initServerConfig() {
862 863
     server.dbfilename = "dump.rdb";
863 864
     server.requirepass = NULL;
864 865
     server.shareobjects = 0;
  866
+    server.maxclients = 0;
865 867
     ResetServerSaveParams();
866 868
 
867 869
     appendServerSaveParams(60*60,1);  /* save after 1 hour and 1 change */
@@ -1020,6 +1022,8 @@ static void loadServerConfig(char *filename) {
1020 1022
             if (server.dbnum < 1) {
1021 1023
                 err = "Invalid number of databases"; goto loaderr;
1022 1024
             }
  1025
+        } else if (!strcasecmp(argv[0],"maxclients") && argc == 2) {
  1026
+            server.maxclients = atoi(argv[1]);
1023 1027
         } else if (!strcasecmp(argv[0],"slaveof") && argc == 3) {
1024 1028
             server.masterhost = sdsnew(argv[1]);
1025 1029
             server.masterport = atoi(argv[2]);
@@ -1500,6 +1504,7 @@ static void addReplySds(redisClient *c, sds s) {
1500 1504
 static void acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
1501 1505
     int cport, cfd;
1502 1506
     char cip[128];
  1507
+    redisClient *c;
1503 1508
     REDIS_NOTUSED(el);
1504 1509
     REDIS_NOTUSED(mask);
1505 1510
     REDIS_NOTUSED(privdata);
@@ -1510,11 +1515,23 @@ static void acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
1510 1515
         return;
1511 1516
     }
1512 1517
     redisLog(REDIS_DEBUG,"Accepted %s:%d", cip, cport);
1513  
-    if (createClient(cfd) == NULL) {
  1518
+    if ((c = createClient(cfd)) == NULL) {
1514 1519
         redisLog(REDIS_WARNING,"Error allocating resoures for the client");
1515 1520
         close(cfd); /* May be already closed, just ingore errors */
1516 1521
         return;
1517 1522
     }
  1523
+    /* If maxclient directive is set and this is one client more... close the
  1524
+     * connection. Note that we create the client instead to check before
  1525
+     * for this condition, since now the socket is already set in nonblocking
  1526
+     * mode and we can send an error for free using the Kernel I/O */
  1527
+    if (server.maxclients && listLength(server.clients) > server.maxclients) {
  1528
+        char *err = "-ERR max number of clients reached\r\n";
  1529
+
  1530
+        /* That's a best effort error message, don't check write errors */
  1531
+        write(c->fd,err,strlen(err));
  1532
+        freeClient(c);
  1533
+        return;
  1534
+    }
1518 1535
     server.stat_numconnections++;
1519 1536
 }
1520 1537
 
10  redis.conf
@@ -78,6 +78,16 @@ databases 16
78 78
 
79 79
 # requirepass foobared
80 80
 
  81
+################################### LIMITS ####################################
  82
+
  83
+# Set the max number of connected clients at the same time. By default there
  84
+# is no limit, and it's up to the number of file descriptors the Redis process
  85
+# is able to open. The special value '0' means no limts.
  86
+# Once the limit is reached Redis will close all the new connections sending
  87
+# an error 'max number of clients reached'.
  88
+
  89
+# maxclients 128
  90
+
81 91
 ############################### ADVANCED CONFIG ###############################
82 92
 
83 93
 # Glue small output buffers together in order to send small replies in a

0 notes on commit 285add5

Please sign in to comment.
Something went wrong with that request. Please try again.