Skip to content

Commit

Permalink
redis-cli: cliConnect() flags CC_FORCE and CC_QUIET.
Browse files Browse the repository at this point in the history
We need CC_QUIET in order to fix redis#5096 by silently failing if needed.
  • Loading branch information
antirez committed Jul 9, 2018
1 parent 2a28f06 commit 375589d
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/redis-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@
#define LOG_COLOR_YELLOW "33;1m"
#define LOG_COLOR_RESET "0m"

/* cliConnect() flags. */
#define CC_FORCE (1<<0) /* Re-connect if already connected. */
#define CC_QUIET (1<<1) /* Don't log connecting errors. */

/* --latency-dist palettes. */
int spectrum_palette_color_size = 19;
int spectrum_palette_color[] = {0,233,234,235,237,239,241,243,245,247,144,143,142,184,226,214,208,202,196};
Expand Down Expand Up @@ -741,10 +745,12 @@ static int cliSelect(void) {
return REDIS_ERR;
}

/* Connect to the server. If force is not zero the connection is performed
* even if there is already a connected socket. */
static int cliConnect(int force) {
if (context == NULL || force) {
/* Connect to the server. It is possible to pass certain flags to the function:
* CC_FORCE: The connection is performed even if there is already
* a connected socket.
* CC_QUIET: Don't print errors if connection fails. */
static int cliConnect(int flags) {
if (context == NULL || flags & CC_FORCE) {
if (context != NULL) {
redisFree(context);
}
Expand All @@ -756,11 +762,15 @@ static int cliConnect(int force) {
}

if (context->err) {
fprintf(stderr,"Could not connect to Redis at ");
if (config.hostsocket == NULL)
fprintf(stderr,"%s:%d: %s\n",config.hostip,config.hostport,context->errstr);
else
fprintf(stderr,"%s: %s\n",config.hostsocket,context->errstr);
if (!(flags & CC_QUIET)) {
fprintf(stderr,"Could not connect to Redis at ");
if (config.hostsocket == NULL)
fprintf(stderr,"%s:%d: %s\n",
config.hostip,config.hostport,context->errstr);
else
fprintf(stderr,"%s: %s\n",
config.hostsocket,context->errstr);
}
redisFree(context);
context = NULL;
return REDIS_ERR;
Expand Down Expand Up @@ -1524,7 +1534,7 @@ static int issueCommandRepeat(int argc, char **argv, long repeat) {
while (1) {
config.cluster_reissue_command = 0;
if (cliSendCommand(argc,argv,repeat) != REDIS_OK) {
cliConnect(1);
cliConnect(CC_FORCE);

/* If we still cannot send the command print error.
* We'll try to reconnect the next time. */
Expand All @@ -1535,7 +1545,7 @@ static int issueCommandRepeat(int argc, char **argv, long repeat) {
}
/* Issue the command again if we got redirected in cluster mode */
if (config.cluster_mode && config.cluster_reissue_command) {
cliConnect(1);
cliConnect(CC_FORCE);
} else {
break;
}
Expand Down Expand Up @@ -1698,7 +1708,7 @@ static void repl(void) {
config.hostip = sdsnew(argv[1]);
config.hostport = atoi(argv[2]);
cliRefreshPrompt();
cliConnect(1);
cliConnect(CC_FORCE);
} else if (argc == 1 && !strcasecmp(argv[0],"clear")) {
linenoiseClearScreen();
} else {
Expand Down Expand Up @@ -1822,7 +1832,7 @@ static int evalMode(int argc, char **argv) {
strncpy(config.prompt,"lua debugger> ",sizeof(config.prompt));
repl();
/* Restart the session if repl() returned. */
cliConnect(1);
cliConnect(CC_FORCE);
printf("\n");
}
} else {
Expand Down

0 comments on commit 375589d

Please sign in to comment.