]> git.saurik.com Git - redis.git/commitdiff
code generating the CLIENT LIST output refactored to have a function that is able...
authorantirez <antirez@gmail.com>
Mon, 21 Nov 2011 14:34:32 +0000 (15:34 +0100)
committerantirez <antirez@gmail.com>
Mon, 21 Nov 2011 14:34:32 +0000 (15:34 +0100)
src/networking.c

index edd7891d379dfe99328c8ad5dc74992de9ac38fc..073bf4dfcb928bbf43e61d0039bda6339d392ef6 100644 (file)
@@ -924,6 +924,42 @@ void getClientsMaxBuffers(unsigned long *longest_output_list,
     *biggest_input_buffer = bib;
 }
 
     *biggest_input_buffer = bib;
 }
 
+/* Turn a Redis client into an sds string representing its state. */
+sds getClientInfoString(redisClient *client) {
+    char ip[32], flags[16], *p;
+    int port;
+    time_t now = time(NULL);
+
+    if (anetPeerToString(client->fd,ip,&port) == -1) {
+        ip[0] = '?';
+        ip[1] = '\0';
+        port = 0;
+    }
+    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_DIRTY_CAS) *p++ = 'd';
+    if (client->flags & REDIS_CLOSE_AFTER_REPLY) *p++ = 'c';
+    if (client->flags & REDIS_UNBLOCKED) *p++ = 'u';
+    *p++ = '\0';
+    return sdscatprintf(sdsempty(),
+        "addr=%s:%d fd=%d idle=%ld flags=%s db=%d sub=%d psub=%d",
+        ip,port,client->fd,
+        (long)(now - client->lastinteraction),
+        flags,
+        client->db->id,
+        (int) dictSize(client->pubsub_channels),
+        (int) listLength(client->pubsub_patterns));
+}
+
 void clientCommand(redisClient *c) {
     listNode *ln;
     listIter li;
 void clientCommand(redisClient *c) {
     listNode *ln;
     listIter li;
@@ -931,38 +967,12 @@ void clientCommand(redisClient *c) {
 
     if (!strcasecmp(c->argv[1]->ptr,"list") && c->argc == 2) {
         sds o = sdsempty();
 
     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) {
 
         listRewind(server.clients,&li);
         while ((ln = listNext(&li)) != NULL) {
-            char ip[32], flags[16], *p;
-            int port;
-
             client = listNodeValue(ln);
             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_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));
+            o = sdscatsds(o,getClientInfoString(client));
+            o = sdscatlen(o,"\n",1);
         }
         addReplyBulkCBuffer(c,o,sdslen(o));
         sdsfree(o);
         }
         addReplyBulkCBuffer(c,o,sdslen(o));
         sdsfree(o);