10000 Processing `read_timeout` parameter + update docs · chenyongze/phpredis@b56dc49 · GitHub
[go: up one dir, main page]

Skip to content

Commit b56dc49

Browse files
committed
Processing read_timeout parameter + update docs
1 parent d5a2da8 commit b56dc49

13 files changed

+82
-32
lines changed

README.markdown

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ You can install install it using Homebrew:
8282
phpredis can be used to store PHP sessions. To do this, configure `session.save_handler` and `session.save_path` in your php.ini to tell phpredis where to store the sessions:
8383
~~~
8484
session.save_handler = redis
85-
session.save_path = "tcp://host1:6379?weight=1, tcp://host2:6379?weight=2&timeout=2.5, tcp://host3:6379?weight=2"
85+
session.save_path = "tcp://host1:6379?weight=1, tcp://host2:6379?weight=2&timeout=2.5, tcp://host3:6379?weight=2&read_timeout=2.5"
8686
~~~
8787

8888
`session.save_path` can have a simple `host:port` format too, but you need to provide the `tcp://` scheme if you want to use the parameters. The following parameters are available:
@@ -202,6 +202,7 @@ _**Description**_: Connects to a Redis instance.
202202
*timeout*: float, value in seconds (optional, default is 0 meaning unlimited)
203203
*reserved*: should be NULL if retry_interval is specified
204204
*retry_interval*: int, value in milliseconds (optional)
205+
*read_timeout*: float, value in seconds (optional, default is 0 meaning unlimited)
205206

206207
##### *Return value*
207208

@@ -238,6 +239,7 @@ persistent equivalents.
238239
*timeout*: float, value in seconds (optional, default is 0 meaning unlimited)
239240
*persistent_id*: string. identity for the requested persistent connection
240241
*retry_interval*: int, value in milliseconds (optional)
242+
*read_timeout*: float, value in seconds (optional, default is 0 meaning unlimited)
241243

242244
##### *Return value*
243245

arrays.markdown

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ The connect_timeout value is a double and is used to specify a timeout in number
5555
$ra = new RedisArray(array("host1", "host2:63792", "host2:6380"), array("connect_timeout" => 0.5));
5656
</pre>
5757

58+
#### Specifying the "read_timeout" parameter
59+
The read_timeout value is a double and is used to specify a timeout in number of seconds when waiting response from the server.
60+
<pre>
61+
$ra = new RedisArray(array("host1", "host2:63792", "host2:6380"), array("read_timeout" => 0.5));
62+
</pre>
63+
64+
5865
#### Defining arrays in Redis.ini
5966

6067
Because php.ini parameters must be pre-defined, Redis Arrays must all share the same .ini settings.

cluster.markdown

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@ To maintain consistency with the RedisArray class, one can create and connect to
1313
$obj_cluster = new RedisCluster(NULL, Array('host:7000', 'host:7001', 'host:7003'));
1414

1515
// Connect and specify timeout and read_timeout
16-
$obj_cluster = new RedisCluster(
17-
NULL, Array("host:7000", "host:7001"), 1.5, 1.5
18-
);
16+
$obj_cluster = new RedisCluster(NULL, Array("host:7000", "host:7001"), 1.5, 1.5);
1917

2018
// Connect with read/write timeout as well as specify that phpredis should use
2119
// persistent connections to each node.
22-
$obj_cluster = new RedisCluster(
23-
NULL, Array("host:7000", "host:7001"), 1.5, 1.5, true
24-
);
20+
$obj_cluster = new RedisCluster(NULL, Array("host:7000", "host:7001"), 1.5, 1.5, true);
2521

2622
</pre>
2723

cluster_library.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ cluster_node_create(redisCluster *c, char *host, size_t host_len,
649649

650650
// Attach socket
651651
node->sock = redis_sock_create(host, host_len, port, c->timeout,
652-
c->persistent, NULL, 0, 1);
652+
c->read_timeout, c->persistent, NULL, 0, 1);
653653

654654
return node;
655655
}
@@ -916,7 +916,7 @@ cluster_init_seeds(redisCluster *cluster, HashTable *ht_seeds) {
916916
// Allocate a structure for this seed
917917
redis_sock = redis_sock_create(str, psep-str,
918918
(unsigned short)atoi(psep+1), cluster->timeout,
919-
cluster->persistent, NULL, 0, 0);
919+
cluster->read_timeout, cluster->persistent, NULL, 0, 0);
920920

921921
// Index this seed by host/port
922922
key_len = snprintf(key, sizeof(key), "%s:%u", redis_sock->host,

library.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,9 +1467,10 @@ PHP_REDIS_API void redis_debug_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock
14671467
* redis_sock_create
14681468
*/
14691469
PHP_REDIS_API RedisSock*
1470-
redis_sock_create(char *host, int host_len, unsigned short port, double timeout,
1471-
int persistent, char *persistent_id, long retry_interval,
1472-
zend_bool lazy_connect)
1470+
redis_sock_create(char *host, int host_len, unsigned short port,
1471+
double timeout, double read_timeout,
1472+
int persistent, char *persistent_id,
1473+
long retry_interval, zend_bool lazy_connect)
14731474
{
14741475
RedisSock *redis_sock;
14751476

@@ -1490,7 +1491,7 @@ redis_sock_create(char *host, int host_len, unsigned short port, double timeout,
14901491

14911492
redis_sock->port = port;
14921493
redis_sock->timeout = timeout;
1493-
redis_sock->read_timeout = timeout;
1494+
redis_sock->read_timeout = read_timeout;
14941495

14951496
redis_sock->serializer = REDIS_SERIALIZER_NONE;
14961497
redis_sock->mode = ATOMIC;

library.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ PHP_REDIS_API void redis_info_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *
3333
PHP_REDIS_API void redis_parse_info_response(char *response, zval *z_ret);
3434
PHP_REDIS_API void redis_parse_client_list_response(char *response, zval *z_ret);
3535
PHP_REDIS_API void redis_type_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
36-
PHP_REDIS_API RedisSock* redis_sock_create(char *host, int host_len, unsigned short port, double timeout, int persistent, char *persistent_id, long retry_interval, zend_bool lazy_connect);
36+
PHP_REDIS_API RedisSock* redis_sock_create(char *host, int host_len, unsigned short port, double timeout, double read_timeout, int persistent, char *persistent_id, long retry_interval, zend_bool lazy_connect);
3737
PHP_REDIS_API int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC);
3838
PHP_REDIS_API int redis_sock_server_open(RedisSock *redis_sock TSRMLS_DC);
3939
PHP_REDIS_API int redis_sock_disconnect(RedisSock *redis_sock TSRMLS_DC);

redis.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ redis_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
807807
char *host = NULL, *persistent_id = NULL;
808808
zend_long port = -1, retry_interval = 0;
809809
strlen_t host_len, persistent_id_len;
810-
double timeout = 0.0;
810+
double timeout = 0.0, read_timeout = 0.0;
811811
redis_object *redis;
812812

813813
#ifdef ZTS
@@ -817,19 +817,25 @@ redis_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
817817
#endif
818818

819819
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(),
820-
"Os|ldsl", &object, redis_ce, &host,
820+
"Os|ldsld", &object, redis_ce, &host,
821821
&host_len, &port, &timeout, &persistent_id,
822-
&persistent_id_len, &retry_interval)
823-
== FAILURE)
822+
&persistent_id_len, &retry_interval,
823+
&read_timeout) == FAILURE)
824824
{
825825
return FAILURE;
826826
} else if (!persistent) {
827827
persistent_id = NULL;
828828
}
829829

830830
if (timeout < 0L || timeout > INT_MAX) {
831-
zend_throw_exception(redis_exception_ce, "Invalid timeout",
832-
0 TSRMLS_CC);
831+
zend_throw_exception(redis_exception_ce,
832+
"Invalid connect timeout", 0 TSRMLS_CC);
833+
return FAILURE;
834+
}
835+
836+
if (read_timeout < 0L || read_timeout > INT_MAX) {
837+
zend_throw_exception(redis_exception_ce,
838+
"Invalid read timeout", 0 TSRMLS_CC);
833839
return FAILURE;
834840
}
835841

@@ -855,7 +861,7 @@ redis_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
855861
redis_free_socket(redis->sock);
856862
}
857863

858-
redis->sock = redis_sock_create(host, host_len, port, timeout, persistent,
864+
redis->sock = redis_sock_create(host, host_len, port, timeout, read_timeout, persistent,
859865
persistent_id, retry_interval, 0);
860866

861867
if (redis_sock_server_open(redis->sock TSRMLS_CC) < 0) {

redis_array.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ PHP_METHOD(RedisArray, __construct)
230230
HashTable *hPrev = NULL, *hOpts = NULL;
231231
long l_retry_interval = 0;
232232
zend_bool b_lazy_connect = 0;
233-
double d_connect_timeout = 0;
233+
double d_connect_timeout = 0, read_timeout = 0.0;
234234
redis_array_object *obj;
235235

236236
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|a", &z0, &z_opts) == FAILURE) {
@@ -300,6 +300,17 @@ PHP_METHOD(RedisArray, __construct)
300300
d_connect_timeout = atof(Z_STRVAL_P(zpData));
301301
}
302302
}
303+
304+
/* extract read_timeout option */
305+
if ((zpData = zend_hash_str_find(hOpts, "read_timeout", sizeof("read_timeout") - 1)) != NULL) {
306+
if (Z_TYPE_P(zpData) == IS_DOUBLE) {
307+
read_timeout = Z_DVAL_P(zpData);
308+
} else if (Z_TYPE_P(zpData) == IS_LONG) {
309+
read_timeout = Z_LVAL_P(zpData);
310+
} else if (Z_TYPE_P(zpData) == IS_STRING) {
311+
read_timeout = atof(Z_STRVAL_P(zpData));
312+
}
313+
}
303314
}
304315

305316
/* extract either name of list of hosts from z0 */
@@ -309,7 +320,7 @@ PHP_METHOD(RedisArray, __construct)
309320
break;
310321

311322
case IS_ARRAY:
312-
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);
323+
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, read_timeout TSRMLS_CC);
313324
break;
314325

315326
default:

redis_array.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ typedef struct RedisArray_ {
5252
zval z_dist; /* key distributor, callable */
5353
zval z_pure_cmds; /* hash table */
5454
double connect_timeout; /* socket connect timeout */
55+
double read_timeout; /* socket read timeout */
5556

5657
struct RedisArray_ *prev;
5758
} RedisArray;

redis_array_impl.c

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ ra_load_hosts(RedisArray *ra, HashTable *hosts, long retry_interval, zend_bool b
7979
#endif
8080

8181
/* create socket */
82-
redis->sock = redis_sock_create(host, host_len, port, ra->connect_timeout, ra->pconnect, NULL, retry_interval, b_lazy_connect);
82+
redis->sock = redis_sock_create(host, host_len, port, ra->connect_timeout, ra->read_timeout, ra->pconnect, NULL, retry_interval, b_lazy_connect);
8383

8484
if (!b_lazy_connect)
8585
{
@@ -173,13 +173,14 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
173173
zval z_params_retry_interval;
174174
zval z_params_pconnect;
175175
zval z_params_connect_timeout;
176+
zval z_params_read_timeout;
176177
zval z_params_lazy_connect;
177178
RedisArray *ra = NULL;
178179

179180
zend_bool b_index = 0, b_autorehash = 0, b_pconnect = 0;
180181
long l_retry_interval = 0;
181182
zend_bool b_lazy_connect = 0;
182-
double d_connect_timeout = 0;
183+
double d_connect_timeout = 0, read_timeout = 0.0;
183184
HashTable *hHosts = NULL, *hPrev = NULL;
184185
size_t name_len = strlen(name);
185186
char *iptr;
@@ -297,9 +298,25 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
297298
d_connect_timeout = Z_LVAL_P(z_data);
298299
}
299300
}
301+
302+
/* find read timeout option */
303+
array_init(&z_params_connect_timeout);
304+
if ((iptr = INI_STR("redis.arrays.readtimeout")) != NULL) {
305+
sapi_module.treat_data(PARSE_STRING, estrdup(iptr), &z_params_read_timeout TSRMLS_CC);
306+
}
307+
if ((z_data = zend_hash_str_find(Z_ARRVAL(z_params_read_timeout), name, name_len)) != NULL) {
308+
if (Z_TYPE_P(z_data) == IS_DOUBLE) {
309+
read_timeout = Z_DVAL_P(z_data);
310+
} else if (Z_TYPE_P(z_data) == IS_STRING) {
311+
read_timeout = atof(Z_STRVAL_P(z_data));
312+
} else if (Z_TYPE_P(z_data) == IS_LONG) {
313+
read_timeout = Z_LVAL_P(z_data);
314+
}
315+
}
316+
300317

301318
/* create RedisArray object */
302-
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);
319+
ra = ra_make_array(hHosts, &z_fun, &z_dist, hPrev, b_index, b_pconnect, l_retry_interval, b_lazy_connect, d_connect_timeout, read_timeout TSRMLS_CC);
303320
if (ra) {
304321
ra->auto_rehash = b_autorehash;
305322
if(ra->prev) ra->prev->auto_rehash = b_autorehash;
@@ -315,6 +332,7 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
315332
zval_dtor(&z_params_retry_interval);
316333
zval_dtor(&z_params_pconnect);
317334
zval_dtor(&z_params_connect_timeout);
335+
zval_dtor(&z_params_read_timeout);
318336
zval_dtor(&z_params_lazy_connect);
319337
zval_dtor(&z_dist);
320338
zval_dtor(&z_fun);
@@ -323,7 +341,7 @@ RedisArray *ra_load_array(const char *name TSRMLS_DC) {
323341
}
324342

325343
RedisArray *
326-
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) {
344+
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, double read_timeout TSRMLS_DC) {
327345

328346
int i, count;
329347
RedisArray *ra;
@@ -341,6 +359,7 @@ ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, HashTable *hosts_prev
341359
ra->auto_rehash = 0;
342360
ra->pconnect = b_pconnect;
343361
ra->connect_timeout = connect_timeout;
362+
ra->read_timeout = read_timeout;
344363

345364
if (ra_load_hosts(ra, hosts, retry_interval, b_lazy_connect TSRMLS_CC) == NULL || !ra->count) {
346365
for (i = 0; i < ra->count; ++i) {
@@ -352,7 +371,7 @@ ra_make_array(HashTable *hosts, zval *z_fun, zval *z_dist, HashTable *hosts_prev
352371
efree(ra);
353372
return NULL;
354373
}
355-
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;
374+
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, read_timeout TSRMLS_CC) : NULL;
356375

357376
/* init array data structures */
358377
ra_init_function_table(ra);

0 commit comments

Comments
 (0)
0