]> git.saurik.com Git - redis.git/blobdiff - src/replication.c
unstable version called 2.9.4
[redis.git] / src / replication.c
index 479dcf437c4763970379f7f1d2c4ab4ea78d1133..dd42fcf17f4e39a3857f81cd482403eafd3ee73b 100644 (file)
@@ -517,9 +517,23 @@ void slaveofCommand(redisClient *c) {
             redisLog(REDIS_NOTICE,"MASTER MODE enabled (user request)");
         }
     } else {
+        long port;
+
+        if ((getLongFromObjectOrReply(c, c->argv[2], &port, NULL) != REDIS_OK))
+            return;
+
+        /* Check if we are already attached to the specified slave */
+        if (server.masterhost && !strcasecmp(server.masterhost,c->argv[1]->ptr)
+            && server.masterport == port) {
+            redisLog(REDIS_NOTICE,"SLAVE OF would result into synchronization with the master we are already connected with. No operation performed.");
+            addReplySds(c,sdsnew("+OK Already connected to specified master\r\n"));
+            return;
+        }
+        /* There was no previous master or the user specified a different one,
+         * we can continue. */
         sdsfree(server.masterhost);
         server.masterhost = sdsdup(c->argv[1]->ptr);
-        server.masterport = atoi(c->argv[2]->ptr);
+        server.masterport = port;
         if (server.master) freeClient(server.master);
         if (server.repl_state == REDIS_REPL_TRANSFER)
             replicationAbortSyncTransfer();