]> git.saurik.com Git - redis.git/blobdiff - src/networking.c
Fixed a bug with replication where SLAVEOF NO ONE caused a slave to close the connect...
[redis.git] / src / networking.c
index 32c063060c10b0d9037eeda8dd64a452cedf55ba..29eed88866fb11d26094021cd3a01bb7ad0dd697 100644 (file)
@@ -16,7 +16,6 @@ redisClient *createClient(int fd) {
 
     anetNonBlock(NULL,fd);
     anetTcpNoDelay(NULL,fd);
-    if (!c) return NULL;
     if (aeCreateFileEvent(server.el,fd,AE_READABLE,
         readQueryFromClient, c) == AE_ERR)
     {
@@ -321,7 +320,12 @@ void _addReplyLongLong(redisClient *c, long long ll, char prefix) {
 }
 
 void addReplyLongLong(redisClient *c, long long ll) {
-    _addReplyLongLong(c,ll,':');
+    if (ll == 0)
+        addReply(c,shared.czero);
+    else if (ll == 1)
+        addReply(c,shared.cone);
+    else
+        _addReplyLongLong(c,ll,':');
 }
 
 void addReplyMultiBulkLen(redisClient *c, long length) {
@@ -522,10 +526,16 @@ void freeClient(redisClient *c) {
          * close the connection with all our slaves if we have any, so
          * when we'll resync with the master the other slaves will sync again
          * with us as well. Note that also when the slave is not connected
-         * to the master it will keep refusing connections by other slaves. */
-        while (listLength(server.slaves)) {
-            ln = listFirst(server.slaves);
-            freeClient((redisClient*)ln->value);
+         * to the master it will keep refusing connections by other slaves.
+         *
+         * We do this only if server.masterhost != NULL. If it is NULL this
+         * means the user called SLAVEOF NO ONE and we are freeing our
+         * link with the master, so no need to close link with slaves. */
+        if (server.masterhost != NULL) {
+            while (listLength(server.slaves)) {
+                ln = listFirst(server.slaves);
+                freeClient((redisClient*)ln->value);
+            }
         }
     }
     /* Release memory */
@@ -865,3 +875,70 @@ void getClientsMaxBuffers(unsigned long *longest_output_list,
     *biggest_input_buffer = bib;
 }
 
+void clientCommand(redisClient *c) {
+    listNode *ln;
+    listIter li;
+    redisClient *client;
+
+    if (!strcasecmp(c->argv[1]->ptr,"list") && c->argc == 2) {
+        sds o = sdsempty();
+        time_t now = time(NULL);
+
+        listRewind(server.clients,&li);
+        while ((ln = listNext(&li)) != NULL) {
+            char ip[32], flags[16], *p;
+            int port;
+
+            client = listNodeValue(ln);
+            if (anetPeerToString(client->fd,ip,&port) == -1) continue;
+            p = flags;
+            if (client->flags & REDIS_SLAVE) {
+                if (client->flags & REDIS_MONITOR)
+                    *p++ = 'O';
+                else
+                    *p++ = 'S';
+            }
+            if (client->flags & REDIS_MASTER) *p++ = 'M';
+            if (p == flags) *p++ = 'N';
+            if (client->flags & REDIS_MULTI) *p++ = 'x';
+            if (client->flags & REDIS_BLOCKED) *p++ = 'b';
+            if (client->flags & REDIS_IO_WAIT) *p++ = 'i';
+            if (client->flags & REDIS_DIRTY_CAS) *p++ = 'd';
+            if (client->flags & REDIS_CLOSE_AFTER_REPLY) *p++ = 'c';
+            if (client->flags & REDIS_UNBLOCKED) *p++ = 'u';
+            *p++ = '\0';
+            o = sdscatprintf(o,
+                "addr=%s:%d fd=%d idle=%ld flags=%s db=%d sub=%d psub=%d\n",
+                ip,port,client->fd,
+                (long)(now - client->lastinteraction),
+                flags,
+                client->db->id,
+                (int) dictSize(client->pubsub_channels),
+                (int) listLength(client->pubsub_patterns));
+        }
+        addReplyBulkCBuffer(c,o,sdslen(o));
+        sdsfree(o);
+    } else if (!strcasecmp(c->argv[1]->ptr,"kill") && c->argc == 3) {
+        listRewind(server.clients,&li);
+        while ((ln = listNext(&li)) != NULL) {
+            char ip[32], addr[64];
+            int port;
+
+            client = listNodeValue(ln);
+            if (anetPeerToString(client->fd,ip,&port) == -1) continue;
+            snprintf(addr,sizeof(addr),"%s:%d",ip,port);
+            if (strcmp(addr,c->argv[2]->ptr) == 0) {
+                addReply(c,shared.ok);
+                if (c == client) {
+                    client->flags |= REDIS_CLOSE_AFTER_REPLY;
+                } else {
+                    freeClient(client);
+                }
+                return;
+            }
+        }
+        addReplyError(c,"No such client");
+    } else {
+        addReplyError(c, "Syntax error, try CLIENT (LIST | KILL ip:port)");
+    }
+}