X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/1824e3a3a37eb9618aa487e6e071395758bdaca8..a950a84303038d3365442a48cc2c06f4b5d3f65e:/src/replication.c diff --git a/src/replication.c b/src/replication.c index 479dcf43..e8297839 100644 --- a/src/replication.c +++ b/src/replication.c @@ -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(); @@ -582,7 +596,7 @@ void replicationCron(void) { if (slave->replstate == REDIS_REPL_SEND_BULK) continue; if (slave->replstate == REDIS_REPL_ONLINE) { /* If the slave is online send a normal ping */ - addReplySds(slave,sdsnew("PING\r\n")); + addReplySds(slave,sdsnew("*1\r\n$4\r\nPING\r\n")); } else { /* Otherwise we are in the pre-synchronization stage. * Just a newline will do the work of refreshing the