Skip to content

Commit 0b422f5

Browse files
Add a no_throw option to redis_check_eof
We don't want redis_check_eof to throw an exception in Cluster, as we'll be getting bounced around while the slave election is in process. Added a very simple debugging function for testing stability, etc.
1 parent 51c187f commit 0b422f5

File tree

4 files changed

+34
-20
lines changed

4 files changed

+34
-20
lines changed

cluster_library.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ static void cluster_dump_nodes(redisCluster *c) {
2828
}
2929
}
3030

31+
static void cluster_log(char *fmt, ...)
32+
{
33+
va_list args;
34+
char buffer[1024];
35+
36+
va_start(args, fmt);
37+
vsnprintf(buffer,sizeof(buffer),fmt,args);
38+
va_end(args);
39+
40+
fprintf(stderr, "%s\n", buffer);
41+
}
42+
3143
/* Direct handling of variant replies, in a hiredis like way. These methods
3244
* are used for non userland facing commands, as well as passed through from
3345
* them when the reply is just variant (e.g. eval) */
@@ -710,15 +722,15 @@ static int cluster_send_asking(RedisSock *redis_sock TSRMLS_DC)
710722
char buf[255];
711723

712724
// Make sure we can send the request
713-
if(redis_check_eof(redis_sock TSRMLS_DC) ||
725+
if(redis_check_eof(redis_sock, 1 TSRMLS_DC) ||
714726
php_stream_write(redis_sock->stream, RESP_ASKING_CMD,
715727
sizeof(RESP_ASKING_CMD)-1) != sizeof(RESP_ASKING_CMD)-1)
716728
{
717729
return -1;
718730
}
719731

720732
// Read our reply type
721-
if((redis_check_eof(redis_sock TSRMLS_CC) == - 1) ||
733+
if((redis_check_eof(redis_sock, 1 TSRMLS_CC) == - 1) ||
722734
(reply_type = php_stream_getc(redis_sock->stream TSRMLS_DC)
723735
!= TYPE_LINE))
724736
{
@@ -906,7 +918,7 @@ static int cluster_check_response(redisCluster *c, unsigned short slot,
906918
CLUSTER_CLEAR_ERROR(c);
907919
CLUSTER_CLEAR_REPLY(c);
908920

909-
if(-1 == redis_check_eof(SLOT_SOCK(c,slot)) ||
921+
if(-1 == redis_check_eof(SLOT_SOCK(c,slot), 1 TSRMLS_CC) ||
910922
EOF == (*reply_type = php_stream_getc(SLOT_STREAM(c,slot))))
911923
{
912924
return -1;
@@ -998,7 +1010,7 @@ static int cluster_sock_write(redisCluster *c, unsigned short slot,
9981010

9991011
// First attempt to write it to the slot that's been requested
10001012
if(redis_sock && redis_sock->stream &&
1001-
!redis_check_eof(redis_sock TSRMLS_CC) &&
1013+
!redis_check_eof(redis_sock, 1 TSRMLS_CC) &&
10021014
php_stream_write(redis_sock->stream, cmd, sz)==sz)
10031015
{
10041016
// We were able to write it
@@ -1022,7 +1034,7 @@ static int cluster_sock_write(redisCluster *c, unsigned short slot,
10221034
CLUSTER_LAZY_CONNECT((*seed_node)->sock);
10231035

10241036
// Attempt to write our request to this node
1025-
if(!redis_check_eof((*seed_node)->sock TSRMLS_CC) &&
1037+
if(!redis_check_eof((*seed_node)->sock, 1 TSRMLS_CC) &&
10261038
php_stream_write((*seed_node)->sock->stream, cmd, sz)==sz)
10271039
{
10281040
// Just return the first slot we think this node handles

library.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ PHPAPI void redis_stream_close(RedisSock *redis_sock TSRMLS_DC) {
3434
}
3535
}
3636

37-
PHPAPI int redis_check_eof(RedisSock *redis_sock TSRMLS_DC)
37+
PHPAPI int redis_check_eof(RedisSock *redis_sock, int no_throw TSRMLS_DC)
3838
{
3939
int eof;
4040
int count = 0;
@@ -54,8 +54,10 @@ PHPAPI int redis_check_eof(RedisSock *redis_sock TSRMLS_DC)
5454
redis_sock->status = REDIS_SOCK_STATUS_FAILED;
5555
redis_sock->watching = 0;
5656
}
57-
zend_throw_exception(redis_exception_ce, "Connection lost",
58-
0 TSRMLS_CC);
57+
if(!no_throw) {
58+
zend_throw_exception(redis_exception_ce, "Connection lost",
59+
0 TSRMLS_CC);
60+
}
5961
return -1;
6062
}
6163
if(redis_sock->stream) { /* close existing stream before reconnecting */
@@ -329,7 +331,7 @@ redis_sock_read_multibulk_reply_zval(INTERNAL_FUNCTION_PARAMETERS,
329331
int numElems;
330332
zval *z_tab;
331333

332-
if(-1 == redis_check_eof(redis_sock TSRMLS_CC)) {
334+
if(-1 == redis_check_eof(redis_sock, 0 TSRMLS_CC)) {
333335
return NULL;
334336
}
335337

@@ -368,7 +370,7 @@ PHPAPI char *redis_sock_read_bulk_reply(RedisSock *redis_sock,
368370

369371
char * reply;
370372

371-
if(-1 == redis_check_eof(redis_sock TSRMLS_CC)) {
373+
if(-1 == redis_check_eof(redis_sock, 0 TSRMLS_CC)) {
372374
return NULL;
373375
}
374376

@@ -409,7 +411,7 @@ PHPAPI char *redis_sock_read(RedisSock *redis_sock, int *buf_len TSRMLS_DC)
409411
char *resp = NULL;
410412
size_t err_len;
411413

412-
if(-1 == redis_check_eof(redis_sock TSRMLS_CC)) {
414+
if(-1 == redis_check_eof(redis_sock, 0 TSRMLS_CC)) {
413415
return NULL;
414416
}
415417

@@ -1133,7 +1135,7 @@ redis_sock_read_multibulk_reply_zipped_with_flag(INTERNAL_FUNCTION_PARAMETERS,
11331135
int numElems;
11341136
zval *z_multi_result;
11351137

1136-
if(-1 == redis_check_eof(redis_sock TSRMLS_CC)) {
1138+
if(-1 == redis_check_eof(redis_sock, 0 TSRMLS_CC)) {
11371139
return -1;
11381140
}
11391141
if(php_stream_gets(redis_sock->stream, inbuf, 1024) == NULL) {
@@ -1551,7 +1553,7 @@ PHPAPI int redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAMETERS,
15511553
int numElems;
15521554
zval *z_multi_result;
15531555

1554-
if(-1 == redis_check_eof(redis_sock TSRMLS_CC)) {
1556+
if(-1 == redis_check_eof(redis_sock, 0 TSRMLS_CC)) {
15551557
return -1;
15561558
}
15571559
if(php_stream_gets(redis_sock->stream, inbuf, 1024) == NULL) {
@@ -1602,7 +1604,7 @@ PHPAPI int redis_sock_read_multibulk_reply_raw(INTERNAL_FUNCTION_PARAMETERS,
16021604
int numElems;
16031605
zval *z_multi_result;
16041606

1605-
if(-1 == redis_check_eof(redis_sock TSRMLS_CC)) {
1607+
if(-1 == redis_check_eof(redis_sock, 0 TSRMLS_CC)) {
16061608
return -1;
16071609
}
16081610
if(php_stream_gets(redis_sock->stream, inbuf, 1024) == NULL) {
@@ -1691,7 +1693,7 @@ PHPAPI int redis_sock_read_multibulk_reply_assoc(INTERNAL_FUNCTION_PARAMETERS,
16911693

16921694
zval **z_keys = ctx;
16931695

1694-
if(-1 == redis_check_eof(redis_sock TSRMLS_CC)) {
1696+
if(-1 == redis_check_eof(redis_sock, 0 TSRMLS_CC)) {
16951697
return -1;
16961698
}
16971699
if(php_stream_gets(redis_sock->stream, inbuf, 1024) == NULL) {
@@ -1763,7 +1765,7 @@ PHPAPI int redis_sock_write(RedisSock *redis_sock, char *cmd, size_t sz
17631765
0 TSRMLS_CC);
17641766
return -1;
17651767
}
1766-
if(-1 == redis_check_eof(redis_sock TSRMLS_CC)) {
1768+
if(-1 == redis_check_eof(redis_sock, 0 TSRMLS_CC)) {
17671769
return -1;
17681770
}
17691771
return php_stream_write(redis_sock->stream, cmd, sz);
@@ -1953,7 +1955,7 @@ redis_sock_gets(RedisSock *redis_sock, char *buf, int buf_size,
19531955
size_t *line_size TSRMLS_DC)
19541956
{
19551957
// Handle EOF
1956-
if(-1 == redis_check_eof(redis_sock TSRMLS_CC)) {
1958+
if(-1 == redis_check_eof(redis_sock, 0 TSRMLS_CC)) {
19571959
return -1;
19581960
}
19591961

@@ -1986,7 +1988,7 @@ redis_read_reply_type(RedisSock *redis_sock, REDIS_REPLY_TYPE *reply_type,
19861988
int *reply_info TSRMLS_DC)
19871989
{
19881990
// Make sure we haven't lost the connection, even trying to reconnect
1989-
if(-1 == redis_check_eof(redis_sock TSRMLS_CC)) {
1991+
if(-1 == redis_check_eof(redis_sock, 0 TSRMLS_CC)) {
19901992
// Failure
19911993
return -1;
19921994
}

library.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ PHPAPI int redis_unsubscribe_response(INTERNAL_FUNCTION_PARAMETERS,
4848

4949
PHPAPI int redis_sock_write(RedisSock *redis_sock, char *cmd, size_t sz TSRMLS_DC);
5050
PHPAPI void redis_stream_close(RedisSock *redis_sock TSRMLS_DC);
51-
PHPAPI int redis_check_eof(RedisSock *redis_sock TSRMLS_DC);
51+
PHPAPI int redis_check_eof(RedisSock *redis_sock, int no_throw TSRMLS_DC);
5252
PHPAPI int redis_sock_get(zval *id, RedisSock **redis_sock TSRMLS_DC, int nothrow);
5353
PHPAPI void redis_free_socket(RedisSock *redis_sock);
5454
PHPAPI void redis_send_discard(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock);

redis.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2372,7 +2372,7 @@ PHPAPI int redis_sock_read_multibulk_multi_reply(INTERNAL_FUNCTION_PARAMETERS,
23722372
int numElems;
23732373
zval *z_tab;
23742374

2375-
redis_check_eof(redis_sock TSRMLS_CC);
2375+
redis_check_eof(redis_sock, 0 TSRMLS_CC);
23762376

23772377
php_stream_gets(redis_sock->stream, inbuf, 1024);
23782378
if(inbuf[0] != '*') {

0 commit comments

Comments
 (0)