Sitelet https://github.com/phpredis/phpredis/commit/90cb69f3
Skip to content

Commit 90cb69f

Browse files
yatsukhnenkomichael-grunder
authored andcommitted
Add more connection parameters
1 parent c94e28f commit 90cb69f

1 file changed

Lines changed: 44 additions & 6 deletions

File tree

‎redis_sentinel.c‎

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
#include "php_redis.h"
22
#include "redis_commands.h"
33
#include "redis_sentinel.h"
4+
#include <zend_exceptions.h>
45

56
zend_class_entry *redis_sentinel_ce;
7+
extern zend_class_entry *redis_exception_ce;
68

79
ZEND_BEGIN_ARG_INFO_EX(arginfo_ctor, 0, 0, 1)
810
ZEND_ARG_INFO(0, host)
911
ZEND_ARG_INFO(0, port)
12+
ZEND_ARG_INFO(0, timeout)
13+
ZEND_ARG_INFO(0, persistent)
14+
ZEND_ARG_INFO(0, retry_interval)
15+
ZEND_ARG_INFO(0, read_timeout)
1016
ZEND_END_ARG_INFO()
1117

1218
zend_function_entry redis_sentinel_functions[] = {
@@ -26,21 +32,53 @@ zend_function_entry redis_sentinel_functions[] = {
2632

2733
PHP_METHOD(RedisSentinel, __construct)
2834
{
35+
int persistent = 0;
36+
char *persistent_id = NULL;
37+
double timeout = 0.0, read_timeout = 0.0;
38+
zend_long port = 26379, retry_interval = 0;
2939
redis_sentinel_object *obj;
30-
zend_long port = -1;
3140
zend_string *host;
41+
zval *zv = NULL;
3242

33-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|l", &host, &port) == FAILURE) {
43+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|ldz!ld",
44+
&host, &port, &timeout, &zv,
45+
&retry_interval, &read_timeout) == FAILURE) {
3446
RETURN_FALSE;
3547
}
3648

37-
/* If it's not a unix socket, set to default */
38-
if (port < 0 && ZSTR_LEN(host) > 0 && *ZSTR_VAL(host) != '/') {
39-
port = 26379;
49+
if (port < 0 || port > UINT16_MAX) {
50+
REDIS_THROW_EXCEPTION("Invalid port", 0);
51+
RETURN_FALSE;
52+
}
53+
54+
if (timeout < 0L || timeout > INT_MAX) {
55+
REDIS_THROW_EXCEPTION("Invalid connect timeout", 0);
56+
RETURN_FALSE;
57+
}
58+
59+
if (read_timeout < 0L || read_timeout > INT_MAX) {
60+
REDIS_THROW_EXCEPTION("Invalid read timeout", 0);
61+
RETURN_FALSE;
62+
}
63+
64+
if (retry_interval < 0L || retry_interval > INT_MAX) {
65+
REDIS_THROW_EXCEPTION("Invalid retry interval", 0);
66+
RETURN_FALSE;
67+
}
68+
69+
if (zv) {
70+
ZVAL_DEREF(zv);
71+
if (Z_TYPE_P(zv) == IS_STRING) {
72+
persistent_id = Z_STRVAL_P(zv);
73+
persistent = 1; /* even empty */
74+
} else {
75+
persistent = zval_is_true(zv);
76+
}
4077
}
4178

4279
obj = PHPREDIS_GET_OBJECT(redis_sentinel_object, getThis());
43-
obj->sock = redis_sock_create(ZSTR_VAL(host), ZSTR_LEN(host), port, 0, 0, 0, NULL, 0);
80+
obj->sock = redis_sock_create(ZSTR_VAL(host), ZSTR_LEN(host), port,
81+
timeout, read_timeout, persistent, persistent_id, retry_interval);
4482
}
4583

4684
PHP_METHOD(RedisSentinel, ckquorum)

0 commit comments

Comments
 (0)