diff --git a/src/redis-cli.c b/src/redis-cli.c index 3b5869433..cc1c10193 100644 --- a/src/redis-cli.c +++ b/src/redis-cli.c @@ -793,13 +793,19 @@ static int cliAuth(redisContext *ctx, char *user, char *auth) { reply = redisCommand(ctx,"AUTH %s",auth); else reply = redisCommand(ctx,"AUTH %s %s",user,auth); - if (reply != NULL) { - if (reply->type == REDIS_REPLY_ERROR) - fprintf(stderr,"Warning: AUTH failed\n"); - freeReplyObject(reply); - return REDIS_OK; + + if (reply == NULL) { + fprintf(stderr, "\nI/O error\n"); + return REDIS_ERR; } - return REDIS_ERR; + + int result = REDIS_OK; + if (reply->type == REDIS_REPLY_ERROR) { + result = REDIS_ERR; + fprintf(stderr, "AUTH failed: %s\n", reply->str); + } + freeReplyObject(reply); + return result; } /* Send SELECT input_dbnum to the server */ @@ -808,19 +814,21 @@ static int cliSelect(void) { if (config.input_dbnum == config.dbnum) return REDIS_OK; reply = redisCommand(context,"SELECT %d",config.input_dbnum); - if (reply != NULL) { - int result = REDIS_OK; - if (reply->type == REDIS_REPLY_ERROR) { - result = REDIS_ERR; - fprintf(stderr,"SELECT %d failed: %s\n",config.input_dbnum,reply->str); - } else { - config.dbnum = config.input_dbnum; - cliRefreshPrompt(); - } - freeReplyObject(reply); - return result; + if (reply == NULL) { + fprintf(stderr, "\nI/O error\n"); + return REDIS_ERR; } - return REDIS_ERR; + + int result = REDIS_OK; + if (reply->type == REDIS_REPLY_ERROR) { + result = REDIS_ERR; + fprintf(stderr,"SELECT %d failed: %s\n",config.input_dbnum,reply->str); + } else { + config.dbnum = config.input_dbnum; + cliRefreshPrompt(); + } + freeReplyObject(reply); + return result; } /* Select RESP3 mode if redis-cli was started with the -3 option. */ @@ -829,16 +837,18 @@ static int cliSwitchProto(void) { if (config.resp3 == 0) return REDIS_OK; reply = redisCommand(context,"HELLO 3"); - if (reply != NULL) { - int result = REDIS_OK; - if (reply->type == REDIS_REPLY_ERROR) { - result = REDIS_ERR; - fprintf(stderr,"HELLO 3 failed: %s\n",reply->str); - } - freeReplyObject(reply); - return result; + if (reply == NULL) { + fprintf(stderr, "\nI/O error\n"); + return REDIS_ERR; } - return REDIS_ERR; + + int result = REDIS_OK; + if (reply->type == REDIS_REPLY_ERROR) { + result = REDIS_ERR; + fprintf(stderr,"HELLO 3 failed: %s\n",reply->str); + } + freeReplyObject(reply); + return result; } /* Connect to the server. It is possible to pass certain flags to the function: @@ -875,12 +885,15 @@ static int cliConnect(int flags) { if (context->err) { if (!(flags & CC_QUIET)) { fprintf(stderr,"Could not connect to Redis at "); - if (config.hostsocket == NULL) - fprintf(stderr,"%s:%d: %s\n", + if (config.hostsocket == NULL || + (config.cluster_mode && config.cluster_reissue_command)) + { + fprintf(stderr, "%s:%d: %s\n", config.hostip,config.hostport,context->errstr); - else + } else { fprintf(stderr,"%s: %s\n", config.hostsocket,context->errstr); + } } redisFree(context); context = NULL; @@ -1472,7 +1485,7 @@ static int cliSendCommand(int argc, char **argv, long repeat) { } if (config.cluster_reissue_command){ /* If we need to reissue the command, break to prevent a - further 'repeat' number of dud interations */ + further 'repeat' number of dud interactions */ break; } if (config.interval) usleep(config.interval); @@ -2019,9 +2032,12 @@ static int issueCommandRepeat(int argc, char **argv, long repeat) { return REDIS_ERR; } } + /* Issue the command again if we got redirected in cluster mode */ if (config.cluster_mode && config.cluster_reissue_command) { - cliConnect(CC_FORCE); + /* If cliConnect fails, sleep for a while and try again. */ + if (cliConnect(CC_FORCE) != REDIS_OK) + sleep(1); } else { break; }