Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e6b3fe5

Browse files
committed
Backoff settings in constructor
1 parent a5c41ce commit e6b3fe5

4 files changed

Lines changed: 50 additions & 2 deletions

File tree

README.markdown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ $redis = new Redis([
161161
'connectTimeout' => 2.5,
162162
'auth' => ['phpredis', 'phpredis'],
163163
'ssl' => ['verify_peer' => false],
164+
'backoff' => [
165+
'algorithm' => Redis::BACKOFF_ALGORITHM_DECORRELATED_JITTER,
166+
'base' => 500,
167+
'cap' => 750,
168+
],
164169
]);
165170
~~~
166171

library.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2589,6 +2589,44 @@ redis_sock_set_stream_context(RedisSock *redis_sock, zval *options)
25892589
return SUCCESS;
25902590
}
25912591

2592+
PHP_REDIS_API int
2593+
redis_sock_set_backoff(RedisSock *redis_sock, zval *options)
2594+
{
2595+
zend_string *zkey;
2596+
zend_long val;
2597+
zval *z_ele;
2598+
2599+
if (!redis_sock || Z_TYPE_P(options) != IS_ARRAY) {
2600+
return FAILURE;
2601+
}
2602+
2603+
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(options), zkey, z_ele) {
2604+
if (zkey != NULL) {
2605+
ZVAL_DEREF(z_ele);
2606+
if (zend_string_equals_literal_ci(zkey, "algorithm")) {
2607+
if ((val = zval_get_long(z_ele)) < 0 || val >= REDIS_BACKOFF_ALGORITHMS) {
2608+
return FAILURE;
2609+
}
2610+
redis_sock->backoff.algorithm = val;
2611+
} else if (zend_string_equals_literal_ci(zkey, "base")) {
2612+
if ((val = zval_get_long(z_ele)) < 0) {
2613+
return FAILURE;
2614+
}
2615+
redis_sock->backoff.base = val * 1000;
2616+
} else if (zend_string_equals_literal_ci(zkey, "cap")) {
2617+
if ((val = zval_get_long(z_ele)) < 0) {
2618+
return FAILURE;
2619+
}
2620+
redis_sock->backoff.cap = val * 1000;
2621+
} else {
2622+
php_error_docref(NULL, E_WARNING, "Skip unknown backoff option '%s'", ZSTR_VAL(zkey));
2623+
}
2624+
}
2625+
} ZEND_HASH_FOREACH_END();
2626+
2627+
return SUCCESS;
2628+
}
2629+
25922630
/**
25932631
* redis_sock_read_multibulk_reply
25942632
*/

library.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ PHP_REDIS_API RedisSock *redis_sock_get(zval *id, int nothrow);
117117
PHP_REDIS_API void redis_free_socket(RedisSock *redis_sock);
118118
PHP_REDIS_API void redis_sock_set_err(RedisSock *redis_sock, const char *msg, int msg_len);
119119
PHP_REDIS_API int redis_sock_set_stream_context(RedisSock *redis_sock, zval *options);
120+
PHP_REDIS_API int redis_sock_set_backoff(RedisSock *redis_sock, zval *options);
120121

121122
PHP_REDIS_API int
122123
redis_serialize(RedisSock *redis_sock, zval *z, char **val, size_t *val_len);

redis.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,17 +650,21 @@ PHP_METHOD(Redis, __construct)
650650
}
651651
redis->sock->retry_interval = zval_get_long(val);
652652
} else if (zend_string_equals_literal_ci(zkey, "ssl")) {
653-
if (Z_TYPE_P(val) != IS_ARRAY) {
653+
if (redis_sock_set_stream_context(redis->sock, val) != SUCCESS) {
654654
REDIS_VALUE_EXCEPTION("Invalid SSL context options");
655655
RETURN_THROWS();
656656
}
657-
redis_sock_set_stream_context(redis->sock, val);
658657
} else if (zend_string_equals_literal_ci(zkey, "auth")) {
659658
if (Z_TYPE_P(val) != IS_STRING && Z_TYPE_P(val) != IS_ARRAY) {
660659
REDIS_VALUE_EXCEPTION("Invalid auth credentials");
661660
RETURN_THROWS();
662661
}
663662
redis_sock_set_auth_zval(redis->sock, val);
663+
} else if (zend_string_equals_literal_ci(zkey, "backoff")) {
664+
if (redis_sock_set_backoff(redis->sock, val) != SUCCESS) {
665+
REDIS_VALUE_EXCEPTION("Invalid backoff options");
666+
RETURN_THROWS();
667+
}
664668
} else {
665669
php_error_docref(NULL, E_WARNING, "Skip unknown option '%s'", ZSTR_VAL(zkey));
666670
}

0 commit comments

Comments
 (0)