Skip to content

Commit ff549ab

Browse files
authored
Merge pull request phpredis#1108 from yatsukhnenko/develop
redis_check_eof refactoring
2 parents aa6ff77 + be5c1f5 commit ff549ab

File tree

2 files changed

+41
-36
lines changed

2 files changed

+41
-36
lines changed

library.c

+40-36
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ PHP_REDIS_API void redis_stream_close(RedisSock *redis_sock TSRMLS_DC) {
140140
}
141141
}
142142

143-
PHP_REDIS_API int redis_check_eof(RedisSock *redis_sock, int no_throw TSRMLS_DC)
143+
PHP_REDIS_API int
144+
redis_check_eof(RedisSock *redis_sock, int no_throw TSRMLS_DC)
144145
{
145-
int eof;
146-
int count = 0;
146+
int count;
147147

148148
if (!redis_sock->stream) {
149149
return -1;
@@ -164,53 +164,57 @@ PHP_REDIS_API int redis_check_eof(RedisSock *redis_sock, int no_throw TSRMLS_DC)
164164
* Bug fix of php: https://github.com/php/php-src/pull/1456
165165
* */
166166
errno = 0;
167-
eof = php_stream_eof(redis_sock->stream);
168-
for (; eof; count++) {
169-
if((MULTI == redis_sock->mode) || redis_sock->watching || count == 10) {
170-
/* too many failures */
171-
if(redis_sock->stream) { /* close stream if still here */
172-
REDIS_STREAM_CLOSE_MARK_FAILED(redis_sock);
173-
}
174-
if(!no_throw) {
175-
zend_throw_exception(redis_exception_ce, "Connection lost",
176-
0 TSRMLS_CC);
177-
}
178-
return -1;
167+
if (php_stream_eof(redis_sock->stream) == 0) {
168+
/* Success */
169+
return 0;
170+
} else if (redis_sock->mode == MULTI || redis_sock->watching) {
171+
REDIS_STREAM_CLOSE_MARK_FAILED(redis_sock);
172+
if (!no_throw) {
173+
zend_throw_exception(redis_exception_ce,
174+
"Connection lost and socket is in MULTI/watching mode",
175+
0 TSRMLS_CC);
179176
}
180-
if(redis_sock->stream) { /* close existing stream before reconnecting */
177+
return -1;
178+
}
179+
/* TODO: configurable max retry count */
180+
for (count = 0; count < 10; ++count) {
181+
/* close existing stream before reconnecting */
182+
if (redis_sock->stream) {
181183
redis_stream_close(redis_sock TSRMLS_CC);
182184
redis_sock->stream = NULL;
183-
redis_sock->mode = ATOMIC;
184-
redis_sock->watching = 0;
185185
}
186186
// Wait for a while before trying to reconnect
187187
if (redis_sock->retry_interval) {
188188
// Random factor to avoid having several (or many) concurrent connections trying to reconnect at the same time
189189
long retry_interval = (count ? redis_sock->retry_interval : (php_rand(TSRMLS_C) % redis_sock->retry_interval));
190190
usleep(retry_interval);
191191
}
192-
redis_sock_connect(redis_sock TSRMLS_CC); /* reconnect */
193-
if(redis_sock->stream) { /* check for EOF again. */
192+
/* reconnect */
193+
if (redis_sock_connect(redis_sock TSRMLS_CC) == 0) {
194+
/* check for EOF again. */
194195
errno = 0;
195-
eof = php_stream_eof(redis_sock->stream);
196+
if (php_stream_eof(redis_sock->stream) == 0) {
197+
/* If we're using a password, attempt a reauthorization */
198+
if (redis_sock->auth && resend_auth(redis_sock TSRMLS_CC) != 0) {
199+
break;
200+
}
201+
/* If we're using a non-zero db, reselect it */
202+
if (redis_sock->dbNumber && reselect_db(redis_sock TSRMLS_CC) != 0) {
203+
break;
204+
}
205+
/* Success */
206+
return 0;
207+
}
196208
}
197209
}
198-
199-
/* We've connected if we have a count */
200-
if (count) {
201-
/* If we're using a password, attempt a reauthorization */
202-
if (redis_sock->auth && resend_auth(redis_sock TSRMLS_CC) != 0) {
203-
return -1;
204-
}
205-
206-
/* If we're using a non-zero db, reselect it */
207-
if (redis_sock->dbNumber && reselect_db(redis_sock TSRMLS_CC) != 0) {
208-
return -1;
209-
}
210+
/* close stream if still here */
211+
if (redis_sock->stream) {
212+
REDIS_STREAM_CLOSE_MARK_FAILED(redis_sock);
210213
}
211-
212-
/* Success */
213-
return 0;
214+
if (!no_throw) {
215+
zend_throw_exception(redis_exception_ce, "Connection lost", 0 TSRMLS_CC);
216+
}
217+
return -1;
214218
}
215219

216220

redis.c

+1
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,7 @@ redis_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
844844

845845
if (redis_sock_server_open(redis->sock, 1 TSRMLS_CC) < 0) {
846846
redis_free_socket(redis->sock);
847+
redis->sock = NULL;
847848
return FAILURE;
848849
}
849850

0 commit comments

Comments
 (0)