@@ -140,10 +140,10 @@ PHP_REDIS_API void redis_stream_close(RedisSock *redis_sock TSRMLS_DC) {
140
140
}
141
141
}
142
142
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 )
144
145
{
145
- int eof ;
146
- int count = 0 ;
146
+ int count ;
147
147
148
148
if (!redis_sock -> stream ) {
149
149
return -1 ;
@@ -164,53 +164,57 @@ PHP_REDIS_API int redis_check_eof(RedisSock *redis_sock, int no_throw TSRMLS_DC)
164
164
* Bug fix of php: https://github.com/php/php-src/pull/1456
165
165
* */
166
166
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 );
179
176
}
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 ) {
181
183
redis_stream_close (redis_sock TSRMLS_CC );
182
184
redis_sock -> stream = NULL ;
183
- redis_sock -> mode = ATOMIC ;
184
- redis_sock -> watching = 0 ;
185
185
}
186
186
// Wait for a while before trying to reconnect
187
187
if (redis_sock -> retry_interval ) {
188
188
// Random factor to avoid having several (or many) concurrent connections trying to reconnect at the same time
189
189
long retry_interval = (count ? redis_sock -> retry_interval : (php_rand (TSRMLS_C ) % redis_sock -> retry_interval ));
190
190
usleep (retry_interval );
191
191
}
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. */
194
195
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
+ }
196
208
}
197
209
}
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 );
210
213
}
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 ;
214
218
}
215
219
216
220
0 commit comments