Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added floating-point timeout for better precision.

  • Loading branch information...
commit 25eb12fb2cb4f3c35ca0bc096313b353d834378d 1 parent 853df32
@nicolasff nicolasff authored
View
2  README.markdown
@@ -59,6 +59,7 @@ Connects to a Redis instance.
*host*: string
*port*: int
+*timeout*: float, value in seconds (optional, default is 0 meaning unlimited)
##### *Return Value*
@@ -67,6 +68,7 @@ Connects to a Redis instance.
##### *Example*
$redis->connect('127.0.0.1', 6379);
+$redis->connect('127.0.0.1', 6379, 2.5); // 2.5 sec timeout.
## get
##### *Description*
View
2  common.h
@@ -130,7 +130,7 @@ typedef struct {
php_stream *stream;
char *host;
unsigned short port;
- long timeout;
+ double timeout;
int failed;
int status;
View
8 library.c
@@ -643,7 +643,7 @@ PHPAPI void redis_string_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis
* redis_sock_create
*/
PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short port,
- long timeout)
+ double timeout)
{
RedisSock *redis_sock;
@@ -680,8 +680,10 @@ PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)
redis_sock_disconnect(redis_sock TSRMLS_CC);
}
- tv.tv_sec = redis_sock->timeout;
- tv.tv_usec = 0;
+ tv.tv_sec = (time_t)redis_sock->timeout;
+ tv.tv_usec = (redis_sock->timeout - tv.tv_sec) * 1000000;
+
+ printf("tv_sec=%d, tv_usec=%d\n", (int)tv.tv_sec, (int)tv.tv_usec);
host_len = spprintf(&host, 0, "%s:%d", redis_sock->host, redis_sock->port);
View
2  library.h
@@ -12,7 +12,7 @@ PHPAPI void redis_bulk_double_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *
PHPAPI void redis_string_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
PHPAPI void redis_info_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
PHPAPI void redis_type_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
-PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short port, long timeout);
+PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short port, double timeout);
PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC);
PHPAPI int redis_sock_server_open(RedisSock *redis_sock, int force_connect TSRMLS_DC);
PHPAPI int redis_sock_disconnect(RedisSock *redis_sock TSRMLS_DC);
View
2  php_redis.h
@@ -191,7 +191,7 @@ struct redis {
};
-#define PHP_REDIS_VERSION "2.0.4"
+#define PHP_REDIS_VERSION "2.0.5"
#endif
View
12 redis.c
@@ -342,7 +342,7 @@ PHP_METHOD(Redis, __construct)
}
/* }}} */
-/* {{{ proto boolean Redis::connect(string host, int port [, int timeout])
+/* {{{ proto boolean Redis::connect(string host, int port [, double timeout])
*/
PHP_METHOD(Redis, connect)
{
@@ -351,21 +351,21 @@ PHP_METHOD(Redis, connect)
char *host = NULL;
long port;
- struct timeval timeout = {0L, 0L};
+ double timeout = 0.0;
RedisSock *redis_sock = NULL;
- if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osl|l",
+ if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osl|d",
&object, redis_ce, &host, &host_len, &port,
- &timeout.tv_sec) == FAILURE) {
+ &timeout) == FAILURE) {
RETURN_FALSE;
}
- if (timeout.tv_sec < 0L || timeout.tv_sec > INT_MAX) {
+ if (timeout < 0L || timeout > INT_MAX) {
zend_throw_exception(redis_exception_ce, "Invalid timeout", 0 TSRMLS_CC);
RETURN_FALSE;
}
- redis_sock = redis_sock_create(host, host_len, port, timeout.tv_sec);
+ redis_sock = redis_sock_create(host, host_len, port, timeout);
if (redis_sock_server_open(redis_sock, 1 TSRMLS_CC) < 0) {
redis_free_socket(redis_sock);
Please sign in to comment.
Something went wrong with that request. Please try again.