diff --git a/src/cli_common.c b/src/cli_common.c index 7d98addfc9b..772de78bf79 100644 --- a/src/cli_common.c +++ b/src/cli_common.c @@ -424,3 +424,21 @@ sds cliVersion(void) { } return version; } + +/* This is a wrapper to call redisConnect or redisConnectWithTimeout. */ +redisContext *redisConnectWrapper(const char *ip, int port, const struct timeval *tv) { + if (tv == NULL) { + return redisConnect(ip, port); + } else { + return redisConnectWithTimeout(ip, port, *tv); + } +} + +/* This is a wrapper to call redisConnectUnix or redisConnectUnixWithTimeout. */ +redisContext *redisConnectUnixWrapper(const char *path, const struct timeval *tv) { + if (tv == NULL) { + return redisConnectUnix(path); + } else { + return redisConnectUnixWithTimeout(path, *tv); + } +} diff --git a/src/cli_common.h b/src/cli_common.h index 3377eaf3a8f..a425dd3a0ab 100644 --- a/src/cli_common.h +++ b/src/cli_common.h @@ -53,4 +53,7 @@ sds escapeJsonString(sds s, const char *p, size_t len); sds cliVersion(void); +redisContext *redisConnectWrapper(const char *ip, int port, const struct timeval *tv); +redisContext *redisConnectUnixWrapper(const char *path, const struct timeval *tv); + #endif /* __CLICOMMON_H */ diff --git a/src/redis-cli.c b/src/redis-cli.c index 8ecc8d90773..85dd0b43c01 100644 --- a/src/redis-cli.c +++ b/src/redis-cli.c @@ -211,7 +211,7 @@ static int createClusterManagerCommand(char *cmdname, int argc, char **argv); static redisContext *context; static struct config { cliConnInfo conn_info; - struct timeval connect_timeout; + struct timeval *connect_timeout; char *hostsocket; int tls; cliSSLconfig sslconfig; @@ -1649,10 +1649,10 @@ static int cliConnect(int flags) { /* Do not use hostsocket when we got redirected in cluster mode */ if (config.hostsocket == NULL || (config.cluster_mode && config.cluster_reissue_command)) { - context = redisConnectWithTimeout(config.conn_info.hostip, config.conn_info.hostport, - config.connect_timeout); + context = redisConnectWrapper(config.conn_info.hostip, config.conn_info.hostport, + config.connect_timeout); } else { - context = redisConnectUnixWithTimeout(config.hostsocket, config.connect_timeout); + context = redisConnectUnixWrapper(config.hostsocket, config.connect_timeout); } if (!context->err && config.tls) { @@ -2595,8 +2595,8 @@ static redisReply *reconnectingRedisCommand(redisContext *c, const char *fmt, .. fflush(stdout); redisFree(c); - c = redisConnectWithTimeout(config.conn_info.hostip, config.conn_info.hostport, - config.connect_timeout); + c = redisConnectWrapper(config.conn_info.hostip, config.conn_info.hostport, + config.connect_timeout); if (!c->err && config.tls) { const char *err = NULL; if (cliSecureConnection(c, config.sslconfig, &err) == REDIS_ERR && err) { @@ -2658,8 +2658,10 @@ static int parseOptions(int argc, char **argv) { fprintf(stderr, "Invalid connection timeout for -t.\n"); exit(1); } - config.connect_timeout.tv_sec = (long long)seconds; - config.connect_timeout.tv_usec = ((long long)(seconds * 1000000)) % 1000000; + if (config.connect_timeout == NULL) + config.connect_timeout = zmalloc(sizeof(*config.connect_timeout)); + config.connect_timeout->tv_sec = (long long)seconds; + config.connect_timeout->tv_usec = ((long long)(seconds * 1000000)) % 1000000; } else if (!strcmp(argv[i],"-s") && !lastarg) { config.hostsocket = argv[++i]; } else if (!strcmp(argv[i],"-r") && !lastarg) { @@ -4085,7 +4087,7 @@ static int clusterManagerExecTransaction(clusterManagerNode *node, static int clusterManagerNodeConnect(clusterManagerNode *node) { if (node->context) redisFree(node->context); - node->context = redisConnectWithTimeout(node->ip, node->port, config.connect_timeout); + node->context = redisConnectWrapper(node->ip, node->port, config.connect_timeout); if (!node->context->err && config.tls) { const char *err = NULL; if (cliSecureConnection(node->context, config.sslconfig, &err) == REDIS_ERR && err) { @@ -7910,7 +7912,7 @@ static int clusterManagerCommandImport(int argc, char **argv) { char *reply_err = NULL; redisReply *src_reply = NULL; // Connect to the source node. - redisContext *src_ctx = redisConnectWithTimeout(src_ip, src_port, config.connect_timeout); + redisContext *src_ctx = redisConnectWrapper(src_ip, src_port, config.connect_timeout); if (src_ctx->err) { success = 0; fprintf(stderr,"Could not connect to Redis at %s:%d: %s.\n", src_ip, @@ -9845,8 +9847,7 @@ int main(int argc, char **argv) { memset(&config.sslconfig, 0, sizeof(config.sslconfig)); config.conn_info.hostip = sdsnew("127.0.0.1"); config.conn_info.hostport = 6379; - config.connect_timeout.tv_sec = 60; - config.connect_timeout.tv_usec = 0; + config.connect_timeout = NULL; config.hostsocket = NULL; config.repeat = 1; config.interval = 0;