Skip to content

Commit 5bdde8d

Browse files
author
Nicolas Favre-Felix
committed
Added reconnect on timeout.
1 parent eef96ab commit 5bdde8d

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

php_redis.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ typedef struct RedisSock_ {
128128
void
129129
add_constant_long(zend_class_entry *ce, char *name, int value);
130130

131+
PHPAPI void redis_check_eof(RedisSock *redis_sock TSRMLS_DC);
131132
PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short port, long timeout);
132133
PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC);
133134
PHPAPI int redis_sock_disconnect(RedisSock *redis_sock TSRMLS_DC);

redis.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,9 @@ PHPAPI char *redis_sock_read(RedisSock *redis_sock, int *buf_len TSRMLS_DC)
338338
{
339339

340340
char inbuf[1024];
341-
char *resp;
341+
char *resp = NULL;
342342

343+
redis_check_eof(redis_sock TSRMLS_CC);
343344
php_stream_gets(redis_sock->stream, inbuf, 1024);
344345

345346
switch(inbuf[0]) {
@@ -385,6 +386,8 @@ PHPAPI char *redis_sock_read_bulk_reply(RedisSock *redis_sock, int bytes)
385386

386387
char * reply;
387388

389+
redis_check_eof(redis_sock TSRMLS_CC);
390+
388391
if (bytes == -1) {
389392
return NULL;
390393
} else {
@@ -415,6 +418,7 @@ PHPAPI int redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAMETERS,
415418
char inbuf[1024], *response;
416419
int response_len;
417420

421+
redis_check_eof(redis_sock TSRMLS_CC);
418422
php_stream_gets(redis_sock->stream, inbuf, 1024);
419423

420424
if(inbuf[0] != '*') {
@@ -443,10 +447,22 @@ PHPAPI int redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAMETERS,
443447
*/
444448
PHPAPI int redis_sock_write(RedisSock *redis_sock, char *cmd, size_t sz)
445449
{
446-
php_stream_write(redis_sock->stream, cmd, sz);
450+
redis_check_eof(redis_sock TSRMLS_CC);
451+
return php_stream_write(redis_sock->stream, cmd, sz);
452+
447453
return 0;
448454
}
449455

456+
457+
PHPAPI void redis_check_eof(RedisSock *redis_sock TSRMLS_DC) {
458+
int eof = php_stream_eof(redis_sock->stream);
459+
while(eof) {
460+
redis_sock->stream = NULL;
461+
redis_sock_connect(redis_sock TSRMLS_CC);
462+
eof = php_stream_eof(redis_sock->stream);
463+
}
464+
}
465+
450466
/**
451467
* redis_sock_get
452468
*/

0 commit comments

Comments
 (0)