diff --git a/.travis.yml b/.travis.yml index 5a046bc..029a8c5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: php php: - - '7.4' - '8.0' + - '8.1' env: - REDIS_HOST=0.0.0.0 REDIS_PORT=6379 XDEBUG_MODE=coverage @@ -13,7 +13,7 @@ services: before_install: - docker run -d -p 0.0.0.0:6379:6379 --name redis redis:alpine - echo "extension=redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini - - echo '' | pecl install swoole-4.8.12 + - echo '' | pecl install swoole - echo "extension=swoole.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini before_script: diff --git a/composer.json b/composer.json index 9ca6aaa..ccd78f4 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ } }, "require": { - "php": "^7.4 || ^8.0", + "php": "^8.0", "psr/http-message": "^1.0" }, "suggest": { diff --git a/src/Adapters/RedisClusterAdapter.php b/src/Adapters/RedisClusterAdapter.php index eb84a86..f01a956 100644 --- a/src/Adapters/RedisClusterAdapter.php +++ b/src/Adapters/RedisClusterAdapter.php @@ -2,74 +2,8 @@ namespace LeoCarmo\CircuitBreaker\Adapters; -class RedisClusterAdapter implements AdapterInterface +class RedisClusterAdapter extends RedisAdapter { - /** - * @var \Redis - */ - protected $redis; - - /** - * @var string - */ - protected string $redisNamespace; - - /** - * Set settings for start circuit service - * - * @param $redis - * @param string $redisNamespace - */ - public function __construct($redis, string $redisNamespace) - { - $this->checkExtensionLoaded(); - $this->redis = $redis; - $this->redisNamespace = $redisNamespace; - } - - protected function checkExtensionLoaded(): void - { - if (! extension_loaded('redis')) { - throw new \RuntimeException('Extension redis is required to use RedisAdapter.'); - } - } - - /** - * @param string $service - * @return bool - */ - public function isOpen(string $service): bool - { - return (bool) $this->redis->get( - $this->makeNamespace($service) . ':open' - ); - } - - /** - * @param string $service - * @param int $failureRateThreshold - * @return bool - */ - public function reachRateLimit(string $service, int $failureRateThreshold): bool - { - $failures = (int) $this->redis->get( - $this->makeNamespace($service) . ':failures' - ); - - return ($failures >= $failureRateThreshold); - } - - /** - * @param string $service - * @return bool - */ - public function isHalfOpen(string $service): bool - { - return (bool) $this->redis->get( - $this->makeNamespace($service) . ':half_open' - ); - } - /** * @param string $service * @param int $timeWindow @@ -98,49 +32,4 @@ public function setSuccess(string $service): void $this->redis->del($serviceName . ':failures'); $this->redis->del($serviceName . ':half_open'); } - - /** - * @param string $service - * @param int $timeWindow - */ - public function setOpenCircuit(string $service, int $timeWindow): void - { - $this->redis->set( - $this->makeNamespace($service) . ':open', - time(), - $timeWindow - ); - } - - /** - * @param string $service - * @param int $timeWindow - * @param int $intervalToHalfOpen - */ - public function setHalfOpenCircuit(string $service, int $timeWindow, int $intervalToHalfOpen): void - { - $this->redis->set( - $this->makeNamespace($service) . ':half_open', - time(), - ($timeWindow + $intervalToHalfOpen) - ); - } - - public function getFailuresCounter(string $service): int - { - $failures = $this->redis->get( - $this->makeNamespace($service) . ':failures' - ); - - return (int) $failures; - } - - /** - * @param string $service - * @return string - */ - protected function makeNamespace(string $service): string - { - return 'circuit-breaker:' . $this->redisNamespace . ':' . $service; - } }