10000 Merge remote-tracking branch 'kotas/opt-read-timeout' · jrtkcoder/phpredis@55dd053 · GitHub
[go: up one dir, main page]

Skip to content

Commit 55dd053

Browse files
Merge remote-tracking branch 'kotas/opt-read-timeout'
Conflicts: library.c
2 parents 85ee61d + 3764a6c commit 55dd053

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ typedef enum _REDIS_REPLY_TYPE {
3535
/* options */
3636
#define REDIS_OPT_SERIALIZER 1
3737
#define REDIS_OPT_PREFIX 2
38+
#define REDIS_OPT_READ_TIMEOUT 3
3839

3940
/* serializers */
4041
#define REDIS_SERIALIZER_NONE 0
@@ -157,6 +158,7 @@ typedef struct {
157158
char *host;
158159
short port;
159160
double timeout;
161+
double read_timeout;
160162
int failed;
161163
int status;
162164
int persistent;

library.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,7 @@ PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short por
861861

862862
redis_sock->port = port;
863863
redis_sock->timeout = timeout;
864+
redis_sock->read_timeout = timeout;
864865

865866
redis_sock->serializer = REDIS_SERIALIZER_NONE;
866867
redis_sock->mode = ATOMIC;
@@ -880,7 +881,7 @@ PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short por
880881
*/
881882
PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)
882883
{
883-
struct timeval tv, *tv_ptr = NULL;
884+
struct timeval tv, read_tv, *tv_ptr = NULL;
884885
char *host = NULL, *persistent_id = NULL, *errstr = NULL;
885886
int host_len, err = 0;
886887
php_netstream_data_t *sock;
@@ -896,6 +897,9 @@ PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)
896897
tv_ptr = &tv;
897898
}
898899

900+
read_tv.tv_sec = (time_t)redis_sock->read_timeout;
901+
read_tv.tv_usec = (int)((redis_sock->read_timeout - read_tv.tv_sec) * 1000000);
902+
899903
if(redis_sock->host[0] == '/' && redis_sock-& 10000 gt;port < 1) {
900904
host_len = spprintf(&host, 0, "unix://%s", redis_sock->host);
901905
} else {
@@ -937,7 +941,7 @@ PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)
937941

938942
if(tv.tv_sec != 0 || tv.tv_usec != 0) {
939943
php_stream_set_option(redis_sock->stream, PHP_STREAM_OPTION_READ_TIMEOUT,
940-
0, &tv);
944+
0, &read_tv);
941945
}
942946
php_stream_set_option(redis_sock->stream,
943947
PHP_STREAM_OPTION_WRITE_BUFFER,

redis.c

Lines changed: 15 additions & 0 deletions
D7AE
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ PHP_MINIT_FUNCTION(redis)
434434
/* options */
435435
add_constant_long(redis_ce, "OPT_SERIALIZER", REDIS_OPT_SERIALIZER);
436436
add_constant_long(redis_ce, "OPT_PREFIX", REDIS_OPT_PREFIX);
437+
add_constant_long(redis_ce, "OPT_READ_TIMEOUT", REDIS_OPT_READ_TIMEOUT);
437438

438439
/* serializer */
439440
add_constant_long(redis_ce, "SERIALIZER_NONE", REDIS_SERIALIZER_NONE);
@@ -5702,6 +5703,9 @@ PHP_METHOD(Redis, getOption) {
57025703
}
57035704
RETURN_NULL();
57045705

5706+
case REDIS_OPT_READ_TIMEOUT:
5707+
RETURN_DOUBLE(redis_sock->read_timeout);
5708+
57055709
default:
57065710
RETURN_FALSE;
57075711

@@ -5717,6 +5721,7 @@ PHP_METHOD(Redis, setOption) {
57175721
long option, val_long;
57185722
char *val_str;
57195723
int val_len;
5724+
struct timeval read_tv;
57205725

57215726
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ols",
57225727
&object, redis_ce, &option, &val_str, &val_len) == FAILURE) {
@@ -5756,6 +5761,16 @@ PHP_METHOD(Redis, setOption) {
57565761
}
57575762
RETURN_TRUE;
57585763

5764+
case REDIS_OPT_READ_TIMEOUT:
5765+
redis_sock->read_timeout = atof(val_str);
5766+
if(redis_sock->stream) {
5767+
read_tv.tv_sec = (time_t)redis_sock->read_timeout;
5768+
read_tv.tv_usec = (int)((redis_sock->read_timeout - read_tv.tv_sec) * 1000000);
5769+
php_stream_set_option(redis_sock->stream, PHP_STREAM_OPTION_READ_TIMEOUT,
5770+
0, &read_tv);
5771+
}
5772+
RETURN_TRUE;
5773+
57595774
default:
57605775
RETURN_FALSE;
57615776
}

tests/TestRedis.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3403,6 +3403,15 @@ public function testTime() {
34033403
strval(intval($time_arr[0])) === strval($time_arr[0]) &&
34043404
strval(intval($time_arr[1])) === strval($time_arr[1]));
34053405
}
3406+
3407+
public function testReadTimeoutOption() {
3408+
3409+
$this->assertTrue(defined('Redis::OPT_READ_TIMEOUT'));
3410+
3411+
$this->redis->setOption(Redis::OPT_READ_TIMEOUT, "12.3");
3412+
$this->assertEquals(12.3, $this->redis->getOption(Redis::OPT_READ_TIMEOUT));
3413+
}
3414+
34063415
}
34073416

34083417
exit(TestSuite::run("Redis_Test"));

0 commit comments

Comments
 (0)
0