Skip to content

Commit

Permalink
By default no timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
enjoy-binbin committed Jan 24, 2024
1 parent 079d23d commit 81051fb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
18 changes: 18 additions & 0 deletions src/cli_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
3 changes: 3 additions & 0 deletions src/cli_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
25 changes: 13 additions & 12 deletions src/redis-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 81051fb

Please sign in to comment.