[memcached] sgrimm, r417: If we run out of open file descriptors, ...

commits at code.sixapart.com commits at code.sixapart.com
Sun Oct 15 08:28:26 UTC 2006


If we run out of open file descriptors, stop listening for new connections
until an existing one closes. This stops the server from chewing excessive
amounts of CPU time when there's a runaway client establishing connections
at a rapid pace.


U   branches/performance/server/memcached.c


Modified: branches/performance/server/memcached.c
===================================================================
--- branches/performance/server/memcached.c	2006-10-15 08:27:06 UTC (rev 416)
+++ branches/performance/server/memcached.c	2006-10-15 08:28:25 UTC (rev 417)
@@ -69,6 +69,7 @@
 static item **todelete = 0;
 static int delcurr;
 static int deltotal;
+static conn *listen_conn;
 
 #define TRANSMIT_COMPLETE   0
 #define TRANSMIT_INCOMPLETE 1
@@ -354,6 +355,7 @@
         fprintf(stderr, "<%d connection closed.\n", c->sfd);
 
     close(c->sfd);
+    accept_new_conns(1);
     conn_cleanup(c);
 
     /* if the connection has big buffers, just free it */
@@ -1343,6 +1345,24 @@
 }
 
 /*
+ * Sets whether we are listening for new connections or not.
+ */
+int accept_new_conns(int do_accept) {
+    if (do_accept) {
+        update_event(listen_conn, EV_READ | EV_PERSIST);
+        if (listen(listen_conn->sfd, 1024)) {
+            perror("listen");
+        }
+    }
+    else {
+        update_event(listen_conn, 0);
+        if (listen(listen_conn->sfd, 0)) {
+            perror("listen");
+        }
+    }
+}
+
+/*
  * Transmit the next chunk of data from our list of msgbuf structures.
  *
  * Returns:
@@ -1422,6 +1442,10 @@
                 if (errno == EAGAIN || errno == EWOULDBLOCK) {
                     exit = 1;
                     break;
+                } else if (errno == EMFILE) {
+                    if (settings.verbose > 0)
+                        fprintf(stderr, "Too many open connections\n");
+                    accept_new_conns(0);
                 } else {
                     perror("accept()");
                 }
@@ -2011,7 +2035,6 @@
 
 int main (int argc, char **argv) {
     int c;
-    conn *l_conn;
     conn *u_conn;
     struct in_addr addr;
     int lock_memory = 0;
@@ -2256,7 +2279,7 @@
         exit(1);
     }
     /* create the initial listening connection */
-    if (!(l_conn = conn_new(l_socket, conn_listening, EV_READ | EV_PERSIST, 1, 0))) {
+    if (!(listen_conn = conn_new(l_socket, conn_listening, EV_READ | EV_PERSIST, 1, 0))) {
         fprintf(stderr, "failed to create listening connection");
         exit(1);
     }




More information about the memcached-commits mailing list