Skip to content

Commit 25eb12f

Browse files
committed
Added floating-point timeout for better precision.
1 parent 853df32 commit 25eb12f

File tree

6 files changed

+16
-12
lines changed

6 files changed

+16
-12
lines changed

README.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Connects to a Redis instance.
5959

6060
*host*: string
6161
*port*: int
62+
*timeout*: float, value in seconds (optional, default is 0 meaning unlimited)
6263

6364
##### *Return Value*
6465

@@ -67,6 +68,7 @@ Connects to a Redis instance.
6768
##### *Example*
6869

6970
$redis->connect('127.0.0.1', 6379);
71+
$redis->connect('127.0.0.1', 6379, 2.5); // 2.5 sec timeout.
7072

7173
## get
7274
##### *Description*

common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ typedef struct {
130130
php_stream *stream;
131131
char *host;
132132
unsigned short port;
133-
long timeout;
133+
double timeout;
134134
int failed;
135135
int status;
136136

library.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ PHPAPI void redis_string_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis
643643
* redis_sock_create
644644
*/
645645
PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short port,
646-
long timeout)
646+
double timeout)
647647
{
648648
RedisSock *redis_sock;
649649

@@ -680,8 +680,10 @@ PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)
680680
redis_sock_disconnect(redis_sock TSRMLS_CC);
681681
}
682682

683-
tv.tv_sec = redis_sock->timeout;
684-
tv.tv_usec = 0;
683+
tv.tv_sec = (time_t)redis_sock->timeout;
684+
tv.tv_usec = (redis_sock->timeout - tv.tv_sec) * 1000000;
685+
686+
printf("tv_sec=%d, tv_usec=%d\n", (int)tv.tv_sec, (int)tv.tv_usec);
685687

686688
host_len = spprintf(&host, 0, "%s:%d", redis_sock->host, redis_sock->port);
687689

library.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ PHPAPI void redis_bulk_double_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *
1212
PHPAPI void redis_string_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
1313
PHPAPI void redis_info_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
1414
PHPAPI void redis_type_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
15-
PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short port, long timeout);
15+
PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short port, double timeout);
1616
PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC);
1717
PHPAPI int redis_sock_server_open(RedisSock *redis_sock, int force_connect TSRMLS_DC);
1818
PHPAPI int redis_sock_disconnect(RedisSock *redis_sock TSRMLS_DC);

php_redis.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ struct redis {
191191
};
192192

193193

194-
#define PHP_REDIS_VERSION "2.0.4"
194+
#define PHP_REDIS_VERSION "2.0.5"
195195

196196
#endif
197197

redis.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ PHP_METHOD(Redis, __construct)
342342
}
343343
/* }}} */
344344

345-
/* {{{ proto boolean Redis::connect(string host, int port [, int timeout])
345+
/* {{{ proto boolean Redis::connect(string host, int port [, double timeout])
346346
*/
347347
PHP_METHOD(Redis, connect)
348348
{
@@ -351,21 +351,21 @@ PHP_METHOD(Redis, connect)
351351
char *host = NULL;
352352
long port;
353353

354-
struct timeval timeout = {0L, 0L};
354+
double timeout = 0.0;
355355
RedisSock *redis_sock = NULL;
356356

357-
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osl|l",
357+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osl|d",
358358
&object, redis_ce, &host, &host_len, &port,
359-
&timeout.tv_sec) == FAILURE) {
359+
&timeout) == FAILURE) {
360360
RETURN_FALSE;
361361
}
362362

363-
if (timeout.tv_sec < 0L || timeout.tv_sec > INT_MAX) {
363+
if (timeout < 0L || timeout > INT_MAX) {
364364
zend_throw_exception(redis_exception_ce, "Invalid timeout", 0 TSRMLS_CC);
365365
RETURN_FALSE;
366366
}
367367

368-
redis_sock = redis_sock_create(host, host_len, port, timeout.tv_sec);
368+
redis_sock = redis_sock_create(host, host_len, port, timeout);
369369

370370
if (redis_sock_server_open(redis_sock, 1 TSRMLS_CC) < 0) {
371371
redis_free_socket(redis_sock);

0 commit comments

Comments
 (0)