Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 07ef7f4

Browse files
Make our timeout or response error handling more explicit.
Although a -1 return value from cluster_check_response is likely a timeout, it is not the only possibility, so handle the loop timeout and error response in distinct ways.
1 parent 27df922 commit 07ef7f4

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

cluster_library.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,8 +1419,11 @@ PHP_REDIS_API short cluster_send_command(redisCluster *c, short slot, const char
14191419
return -1;
14201420
}
14211421

1422-
/* Now check the response from the node we queried. */
1422+
/* Check response and short-circuit on success or communication error */
14231423
resp = cluster_check_response(c, &c->reply_type TSRMLS_CC);
1424+
if (resp == 0 || resp == -1) {
1425+
break;
1426+
}
14241427

14251428
/* Handle MOVED or ASKING redirection */
14261429
if (resp == 1) {
@@ -1439,22 +1442,28 @@ PHP_REDIS_API short cluster_send_command(redisCluster *c, short slot, const char
14391442
}
14401443
}
14411444

1442-
/* We're timed out if cluster_check_response returned -1, or if the
1443-
* response was non-zero and we've been in the loop too long */
1444-
timedout = resp == -1 || (resp && c->waitms ? mstime() - msstart >= c->waitms : 0);
1445-
} while (resp != 0 && !c->clusterdown && !timedout);
1445+
/* See if we've timed out in the command loop */
1446+
timedout = c->waitms ? mstime() - msstart >= c->waitms : 0;
1447+
} while (!c->clusterdown && !timedout);
14461448

14471449
// If we've detected the cluster is down, throw an exception
14481450
if (c->clusterdown) {
14491451
zend_throw_exception(redis_cluster_exception_ce,
14501452
"The Redis Cluster is down (CLUSTERDOWN)", 0 TSRMLS_CC);
14511453
return -1;
1452-
} else if (timedout) {
1454+
} else if (timedout || resp == -1) {
14531455
// Make sure the socket is reconnected, it such that it is in a clean state
14541456
redis_sock_disconnect(c->cmd_sock, 1 TSRMLS_CC);
14551457

1456-
zend_throw_exception(redis_cluster_exception_ce,
1457-
"Timed out attempting to find data in the correct node!", 0 TSRMLS_CC);
1458+
if (timedout) {
1459+
zend_throw_exception(redis_cluster_exception_ce,
1460+
"Timed out attempting to find data in the correct node!", 0 TSRMLS_CC);
1461+
} else {
1462+
zend_throw_exception(redis_cluster_exception_ce,
1463+
"Error processing response from Redis node!", 0 TSRMLS_CC);
1464+
}
1465+
1466+
return -1;
14581467
}
14591468

14601469
/* Clear redirection flag */

0 commit comments

Comments
 (0)