Skip to content

Commit cd6ebc6

Browse files
marcdejongeyatsukhnenko
authored andcommitted
Reset the socket after a timeout to make sure no wrong data is received (phpredis#1417)
* Reset the socket after a timeout to make sure no wrong data is received * Remove the lazy_connect completely * Missing TSRMLS_CC * Remove redundant check if the stream exists * Add the redis_sock_server_open to the CLUSTER_SEND_PAYLOAD macro
1 parent 1b7fe98 commit cd6ebc6

File tree

5 files changed

+11
-28
lines changed

5 files changed

+11
-28
lines changed

cluster_library.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,7 @@ static int cluster_send_direct(RedisSock *redis_sock, char *cmd, int cmd_len,
270270
{
271271
char buf[1024];
272272

273-
/* Connect to the socket if we aren't yet */
274-
CLUSTER_LAZY_CONNECT(redis_sock);
275-
276-
/* Send our command, validate the reply type, and consume the first line */
273+
/* Connect to the socket if we aren't yet and send our command, validate the reply type, and consume the first line */
277274
if (!CLUSTER_SEND_PAYLOAD(redis_sock,cmd,cmd_len) ||
278275
!CLUSTER_VALIDATE_REPLY_TYPE(redis_sock, type) ||
279276
!php_stream_gets(redis_sock->stream, buf, sizeof(buf))) return -1;
@@ -1088,7 +1085,6 @@ PHP_REDIS_API void cluster_disconnect(redisCluster *c, int force TSRMLS_DC) {
10881085
ZEND_HASH_FOREACH_PTR(c->nodes, node) {
10891086
if (node == NULL) continue;
10901087
redis_sock_disconnect(node->sock, force TSRMLS_CC);
1091-
node->sock->lazy_connect = 1;
10921088
} ZEND_HASH_FOREACH_END();
10931089
}
10941090

@@ -1124,7 +1120,9 @@ static int cluster_dist_write(redisCluster *c, const char *cmd, size_t sz,
11241120
if (!redis_sock) continue;
11251121

11261122
/* Connect to this node if we haven't already */
1127-
CLUSTER_LAZY_CONNECT(redis_sock);
1123+
if(redis_sock_server_open(redis_sock TSRMLS_CC)) {
1124+
continue;
1125+
}
11281126

11291127
/* If we're not on the master, attempt to send the READONLY command to
11301128
* this slave, and skip it if that fails */
@@ -1200,11 +1198,9 @@ static int cluster_sock_write(redisCluster *c, const char *cmd, size_t sz,
12001198
* at random. */
12011199
if (failover == REDIS_FAILOVER_NONE) {
12021200
/* Success if we can send our payload to the master */
1203-
CLUSTER_LAZY_CONNECT(redis_sock);
12041201
if (CLUSTER_SEND_PAYLOAD(redis_sock, cmd, sz)) return 0;
12051202
} else if (failover == REDIS_FAILOVER_ERROR) {
12061203
/* Try the master, then fall back to any slaves we may have */
1207-
CLUSTER_LAZY_CONNECT(redis_sock);
12081204
if (CLUSTER_SEND_PAYLOAD(redis_sock, cmd, sz) ||
12091205
!cluster_dist_write(c, cmd, sz, 1 TSRMLS_CC)) return 0;
12101206
} else {
@@ -1225,10 +1221,7 @@ static int cluster_sock_write(redisCluster *c, const char *cmd, size_t sz,
12251221
/* Skip this node if it's the one that failed, or if it's a slave */
12261222
if (seed_node == NULL || seed_node->sock == redis_sock || seed_node->slave) continue;
12271223

1228-
/* Connect to this node if we haven't already */
1229-
CLUSTER_LAZY_CONNECT(seed_node->sock);
1230-
1231-
/* Attempt to write our request to this node */
1224+
/* Connect to this node if we haven't already and attempt to write our request to this node */
12321225
if (CLUSTER_SEND_PAYLOAD(seed_node->sock, cmd, sz)) {
12331226
c->cmd_slot = seed_node->slot;
12341227
c->cmd_sock = seed_node->sock;
@@ -1456,6 +1449,9 @@ PHP_REDIS_API short cluster_send_command(redisCluster *c, short slot, const char
14561449
"The Redis Cluster is down (CLUSTERDOWN)", 0 TSRMLS_CC);
14571450
return -1;
14581451
} else if (timedout) {
1452+
// Make sure the socket is reconnected, it such that it is in a clean state
1453+
redis_sock_disconnect(c->cmd_sock, 1 TSRMLS_CC);
1454+
14591455
zend_throw_exception(redis_cluster_exception_ce,
14601456
"Timed out attempting to find data in the correct node!", 0 TSRMLS_CC);
14611457
}

cluster_library.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,6 @@
5151
ZSTR_LEN(SLOT_SOCK(c,c->redir_slot)->host) != c->redir_host_len || \
5252
memcmp(ZSTR_VAL(SLOT_SOCK(c,c->redir_slot)->host),c->redir_host,c->redir_host_len))
5353

54-
/* Lazy connect logic */
55-
#define CLUSTER_LAZY_CONNECT(s) \
56-
if(s->lazy_connect) { \
57-
s->lazy_connect = 0; \
58-
redis_sock_server_open(s TSRMLS_CC); \
59-
}
60-
6154
/* Clear out our "last error" */
6255
#define CLUSTER_CLEAR_ERROR(c) do { \
6356
if (c->err) { \
@@ -69,7 +62,7 @@
6962

7063
/* Protected sending of data down the wire to a RedisSock->stream */
7164
#define CLUSTER_SEND_PAYLOAD(sock, buf, len) \
72-
(sock && sock->stream && !redis_check_eof(sock, 1 TSRMLS_CC) && \
65+
(sock && !redis_sock_server_open(sock TSRMLS_CC) && sock->stream && !redis_check_eof(sock, 1 TSRMLS_CC) && \
7366
php_stream_write(sock->stream, buf, len)==len)
7467

7568
/* Macro to read our reply type character */

common.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,8 +678,6 @@ typedef struct {
678678

679679
zend_string *err;
680680

681-
zend_bool lazy_connect;
682-
683681
int scan;
684682

685683
int readonly;

library.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1687,7 +1687,6 @@ redis_sock_create(char *host, int host_len, unsigned short port,
16871687
redis_sock->dbNumber = 0;
16881688
redis_sock->retry_interval = retry_interval * 1000;
16891689
redis_sock->persistent = persistent;
1690-
redis_sock->lazy_connect = lazy_connect;
16911690
redis_sock->persistent_id = NULL;
16921691

16931692
if (persistent && persistent_id != NULL) {

redis.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -647,11 +647,8 @@ redis_sock_get(zval *id TSRMLS_DC, int no_throw)
647647
return NULL;
648648
}
649649

650-
if (redis_sock->lazy_connect) {
651-
redis_sock->lazy_connect = 0;
652-
if (redis_sock_server_open(redis_sock TSRMLS_CC) < 0) {
653-
return NULL;
654-
}
650+
if (redis_sock_server_open(redis_sock TSRMLS_CC) < 0) {
651+
return NULL;
655652
}
656653

657654
return redis_sock;

0 commit comments

Comments
 (0)