Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d2bb49f

Browse files
Addreses phpredis#347
Merge branch 'feature/ra_connect_timeout' into develop
2 parents 2e2a84a + 893c15a commit d2bb49f

File tree

4 files changed

+47
-10
lines changed

4 files changed

+47
-10
lines changed

redis_array.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,9 @@ PHP_METHOD(RedisArray, __construct)
196196
RedisArray *ra = NULL;
197197
zend_bool b_index = 0, b_autorehash = 0, b_pconnect = 0;
198198
HashTable *hPrev = NULL, *hOpts = NULL;
199-
long l_retry_interval = 0;
199+
long l_retry_interval = 0;
200200
zend_bool b_lazy_connect = 0;
201+
double d_connect_timeout = 0;
201202

202203
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|a", &z0, &z_opts) == FAILURE) {
203204
RETURN_FALSE;
@@ -261,6 +262,19 @@ PHP_METHOD(RedisArray, __construct)
261262
if(FAILURE != zend_hash_find(hOpts, "lazy_connect", sizeof("lazy_connect"), (void**)&zpData) && Z_TYPE_PP(zpData) == IS_BOOL) {
262263
b_lazy_connect = Z_BVAL_PP(zpData);
263264
}
265+
266+
/* extract connect_timeout option */
267+
zval **z_connect_timeout_pp;
268+
if (FAILURE != zend_hash_find(hOpts, "connect_timeout", sizeof("connect_timeout"), (void**)&z_connect_timeout_pp)) {
269+
if (Z_TYPE_PP(z_connect_timeout_pp) == IS_DOUBLE || Z_TYPE_PP(z_connect_timeout_pp) == IS_STRING) {
270+
if (Z_TYPE_PP(z_connect_timeout_pp) == IS_DOUBLE) {
271+
d_connect_timeout = Z_DVAL_PP(z_connect_timeout_pp);
272+
}
273+
else {
274+
d_connect_timeout = atof(Z_STRVAL_PP(z_connect_timeout_pp));
275+
}
276+
}
277+
}
264278
}
265279

266280
/* extract either name of list of hosts from z0 */
@@ -270,7 +284,7 @@ PHP_METHOD(RedisArray, __construct)
270284
break;
271285

272286
case IS_ARRAY:
273-
ra = ra_make_array(Z_ARRVAL_P(z0), z_fun, z_dist, hPrev, b_index, b_pconnect, l_retry_interval, b_lazy_connect TSRMLS_CC);
287+
ra = ra_make_array(Z_ARRVAL_P(z0), z_fun, z_dist, hPrev, b_index, b_pconnect, l_retry_interval, b_lazy_connect, d_connect_timeout TSRMLS_CC);
274288
break;
275289

276290
default:
@@ -280,6 +294,7 @@ PHP_METHOD(RedisArray, __construct)
280294

281295
if(ra) {
282296
ra->auto_rehash = b_autorehash;
297+
ra->connect_timeout = d_connect_timeout;
283298
#if PHP_VERSION_ID >= 50400
284299
id = zend_list_insert(ra, le_redis_array TSRMLS_CC);
285300
#else

redis_array.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ typedef struct RedisArray_ {
4545
zval *z_fun; /* key extractor, callable */
4646
zval *z_dist; /* key distributor, callable */
4747
zval *z_pure_cmds; /* hash table */
48+
double connect_timeout; /* socket connect timeout */
4849

4950
struct RedisArray_ *prev;
5051
} RedisArray;

redis_array_impl.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ ra_load_hosts(RedisArray *ra, HashTable *hosts, long retry_interval, zend_bool b
7070
call_user_function(&redis_ce->function_table, &ra->redis[i], &z_cons, &z_ret, 0, NULL TSRMLS_CC);
7171

7272
/* create socket */
73-
redis_sock = redis_sock_create(host, host_len, port, 0, ra->pconnect, NULL, retry_interval, b_lazy_connect);
73+
redis_sock = redis_sock_create(host, host_len, port, ra->connect_timeout, ra->pconnect, NULL, retry_interval, b_lazy_connect);
7474

7575
if (!b_lazy_connect)
7676
{
@@ -166,12 +166,14 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
166166
zval *z_params_autorehash;
167167
zval *z_params_retry_interval;
168168
zval *z_params_pconnect;
169+
zval *z_params_connect_timeout;
169170
zval *z_params_lazy_connect;
170171
RedisArray *ra = NULL;
171172

172173
zend_bool b_index = 0, b_autorehash = 0, b_pconnect = 0;
173174
long l_retry_interval = 0;
174175
zend_bool b_lazy_connect = 0;
176+
double d_connect_timeout = 0;
175177
HashTable *hHosts = NULL, *hPrev = NULL;
176178

177179
/* find entry */
@@ -258,18 +260,34 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
258260
b_pconnect = 1;
259261
}
260262
}
261-
/* find retry interval option */
263+
264+
/* find lazy connect option */
262265
MAKE_STD_ZVAL(z_params_lazy_connect);
263266
array_init(z_params_lazy_connect);
264267
sapi_module.treat_data(PARSE_STRING, estrdup(INI_STR("redis.arrays.lazyconnect")), z_params_lazy_connect TSRMLS_CC);
265268
if (zend_hash_find(Z_ARRVAL_P(z_params_lazy_connect), name, strlen(name) + 1, (void **) &z_data_pp) != FAILURE) {
266269
if(Z_TYPE_PP(z_data_pp) == IS_STRING && strncmp(Z_STRVAL_PP(z_data_pp), "1", 1) == 0) {
267-
b_lazy_connect = 1;
270+
b_lazy_connect = 1;
271+
}
272+
}
273+
274+
/* find connect timeout option */
275+
MAKE_STD_ZVAL(z_params_connect_timeout);
276+
array_init(z_params_connect_timeout);
277+
sapi_module.treat_data(PARSE_STRING, estrdup(INI_STR("redis.arrays.connecttimeout")), z_params_connect_timeout TSRMLS_CC);
278+
if (zend_hash_find(Z_ARRVAL_P(z_params_connect_timeout), name, strlen(name) + 1, (void **) &z_data_pp) != FAILURE) {
279+
if (Z_TYPE_PP(z_data_pp) == IS_DOUBLE || Z_TYPE_PP(z_data_pp) == IS_STRING) {
280+
if (Z_TYPE_PP(z_data_pp) == IS_DOUBLE) {
281+
d_connect_timeout = Z_DVAL_PP(z_data_pp);
282+
}
283+
else {
284+
d_connect_timeout = atof(Z_STRVAL_PP(z_data_pp));
285+
}
268286
}
269287
}
270-
288+
271289
/* create RedisArray object */
272-
ra = ra_make_array(hHosts, z_fun, z_dist, hPrev, b_index, b_pconnect, l_retry_interval, b_lazy_connect TSRMLS_CC);
290+
ra = ra_make_array(hHosts, z_fun, z_dist, hPrev, b_index, b_pconnect, l_retry_interval, b_lazy_connect, d_connect_timeout TSRMLS_CC);
273291
ra->auto_rehash = b_autorehash;
274292

275293
/* cleanup */
@@ -287,14 +305,16 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
287305
efree(z_params_retry_interval);
288306
zval_dtor(z_params_pconnect);
289307
efree(z_params_pconnect);
308+
zval_dtor(z_params_connect_timeout);
309+
efree(z_params_connect_timeout);
290310
zval_dtor(z_params_lazy_connect);
291311
efree(z_params_lazy_connect);
292312

293313
return ra;
294314
}
295315

296316
RedisArray *
297-
ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, HashTable *hosts_prev, zend_bool b_index, zend_bool b_pconnect, long retry_interval, zend_bool b_lazy_connect TSRMLS_DC) {
317+
ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, HashTable *hosts_prev, zend_bool b_index, zend_bool b_pconnect, long retry_interval, zend_bool b_lazy_connect, double connect_timeout TSRMLS_DC) {
298318

299319
int count = zend_hash_num_elements(hosts);
300320

@@ -309,14 +329,15 @@ ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, HashTable *hosts_prev
309329
ra->index = b_index;
310330
ra->auto_rehash = 0;
311331
ra->pconnect = b_pconnect;
332+
ra->connect_timeout = connect_timeout;
312333

313334
/* init array data structures */
314335
ra_init_function_table(ra);
315336

316337
if(NULL == ra_load_hosts(ra, hosts, retry_interval, b_lazy_connect TSRMLS_CC)) {
317338
return NULL;
318339
}
319-
ra->prev = hosts_prev ? ra_make_array(hosts_prev, z_fun, z_dist, NULL, b_index, b_pconnect, retry_interval, b_lazy_connect TSRMLS_CC) : NULL;
340+
ra->prev = hosts_prev ? ra_make_array(hosts_prev, z_fun, z_dist, NULL, b_index, b_pconnect, retry_interval, b_lazy_connect, connect_timeout TSRMLS_CC) : NULL;
320341

321342
/* copy function if provided */
322343
if(z_fun) {

redis_array_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
RedisArray *ra_load_hosts(RedisArray *ra, HashTable *hosts, long retry_interval, zend_bool b_lazy_connect TSRMLS_DC);
99
RedisArray *ra_load_array(const char *name TSRMLS_DC);
10-
RedisArray *ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, HashTable *hosts_prev, zend_bool b_index, zend_bool b_pconnect, long retry_interval, zend_bool b_lazy_connect TSRMLS_DC);
10+
RedisArray *ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, HashTable *hosts_prev, zend_bool b_index, zend_bool b_pconnect, long retry_interval, zend_bool b_lazy_connect, double connect_timeout TSRMLS_DC);
1111
zval *ra_find_node_by_name(RedisArray *ra, const char *host, int host_len TSRMLS_DC);
1212
zval *ra_find_node(RedisArray *ra, const char *key, int key_len, int *out_pos TSRMLS_DC);
1313
void ra_init_function_table(RedisArray *ra);

0 commit comments

Comments
 (0)