Skip to content

Commit

Permalink
Merge pull request #16 from leocarmo/update/cluster
Browse files Browse the repository at this point in the history
improve redis cluster adapter
  • Loading branch information
leocarmo authored Dec 14, 2022
2 parents 4f79df2 + 30b1b5c commit 4c9b846
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 115 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
},
"require": {
"php": "^7.4 || ^8.0",
"php": "^8.0",
"psr/http-message": "^1.0"
},
"suggest": {
Expand Down
113 changes: 1 addition & 112 deletions src/Adapters/RedisClusterAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 4c9b846

Please sign in to comment.