|
| 1 | +#include "common.h" |
| 2 | + |
| 3 | +#include <ext/standard/php_rand.h> |
| 4 | + |
| 5 | +#if PHP_VERSION_ID >= 70100 |
| 6 | +#include <ext/standard/php_mt_rand.h> |
| 7 | +#else |
| 8 | +static zend_long php_mt_rand_range(zend_long min, zend_long max) { |
| 9 | + zend_long number = php_rand(); |
| 10 | + RAND_RANGE(number, min, max, PHP_RAND_MAX); |
| 11 | + return number; |
| 12 | +} |
| 13 | +#endif |
| 14 | + |
| 15 | +#include "backoff.h" |
| 16 | + |
| 17 | +static zend_ulong random_range(zend_ulong min, zend_ulong max) { |
| 18 | + if (max < min) { |
| 19 | + return php_mt_rand_range(max, min); |
| 20 | + } |
| 21 | + |
| 22 | + return php_mt_rand_range(min, max); |
| 23 | +} |
| 24 | + |
| 25 | +static zend_ulong redis_default_backoff(struct RedisBackoff *self, unsigned int retry_index) { |
| 26 | + zend_ulong backoff = retry_index ? self->base : random_range(0, self->base); |
| 27 | + return MIN(self->cap, backoff); |
| 28 | +} |
| 29 | + |
| 30 | +static zend_ulong redis_constant_backoff(struct RedisBackoff *self, unsigned int retry_index) { |
| 31 | + zend_ulong backoff = self->base; |
| 32 | + return MIN(self->cap, backoff); |
| 33 | +} |
| 34 | + |
| 35 | +static zend_ulong redis_uniform_backoff(struct RedisBackoff *self, unsigned int retry_index) { |
| 36 | + zend_ulong backoff = random_range(0, self->base); |
| 37 | + return MIN(self->cap, backoff); |
| 38 | +} |
| 39 | + |
| 40 | +static zend_ulong redis_exponential_backoff(struct RedisBackoff *self, unsigned int retry_index) { |
| 41 | + zend_ulong pow = MIN(retry_index, 10); |
| 42 | + zend_ulong backoff = self->base * (1 << pow); |
| 43 | + return MIN(self->cap, backoff); |
| 44 | +} |
| 45 | + |
| 46 | +static zend_ulong redis_full_jitter_backoff(struct RedisBackoff *self, unsigned int retry_index) { |
| 47 | + zend_ulong pow = MIN(retry_index, 10); |
| 48 | + zend_ulong backoff = self->base * (1 << pow); |
| 49 | + zend_ulong cap = MIN(self->cap, backoff); |
| 50 | + return random_range(0, self->cap); |
| 51 | +} |
| 52 | + |
| 53 | +static zend_ulong redis_equal_jitter_backoff(struct RedisBackoff *self, unsigned int retry_index) { |
| 54 | + zend_ulong pow = MIN(retry_index, 10); |
| 55 | + zend_ulong backoff = self->base * (1 << pow); |
| 56 | + zend_ulong temp = MIN(self->cap, backoff); |
| 57 | + return temp / 2 + random_range(0, temp) / 2; |
| 58 | +} |
| 59 | + |
| 60 | +static zend_ulong redis_decorrelated_jitter_backoff(struct RedisBackoff *self, unsigned int retry_index) { |
| 61 | + self->previous_backoff = random_range(self->base, self->previous_backoff * 3); |
| 62 | + return MIN(self->cap, self->previous_backoff); |
| 63 | +} |
| 64 | + |
| 65 | +typedef zend_ulong (*redis_backoff_algorithm)(struct RedisBackoff *self, unsigned int retry_index); |
| 66 | + |
| 67 | +static redis_backoff_algorithm redis_backoff_algorithms[REDIS_BACKOFF_ALGORITHMS] = { |
| 68 | + redis_default_backoff, |
| 69 | + redis_decorrelated_jitter_backoff, |
| 70 | + redis_full_jitter_backoff, |
| 71 | + redis_equal_jitter_backoff, |
| 72 | + redis_exponential_backoff, |
| 73 | + redis_uniform_backoff, |
| 74 | + redis_constant_backoff, |
| 75 | +}; |
| 76 | + |
| 77 | +void redis_initialize_backoff(struct RedisBackoff *self, unsigned long retry_interval) { |
| 78 | + self->algorithm = 0; // default backoff |
| 79 | + self->base = retry_interval; |
| 80 | + self->cap = retry_interval; |
| 81 | + self->previous_backoff = 0; |
| 82 | +} |
| 83 | + |
| 84 | +void redis_backoff_reset(struct RedisBackoff *self) { |
| 85 | + self->previous_backoff = 0; |
| 86 | +} |
| 87 | + |
| 88 | +zend_ulong redis_backoff_compute(struct RedisBackoff *self, unsigned int retry_index) { |
| 89 | + return redis_backoff_algorithms[self->algorithm](self, retry_index); |
| 90 | +} |
0 commit comments