Skip to content

Commit

Permalink
Merge 7d364dc into 4f79df2
Browse files Browse the repository at this point in the history
  • Loading branch information
leocarmo committed Dec 14, 2022
2 parents 4f79df2 + 7d364dc commit fb07320
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 112 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: php
php:
- '7.4'
- '8.0'
- '8.1'

env:
- REDIS_HOST=0.0.0.0 REDIS_PORT=6379 XDEBUG_MODE=coverage
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 fb07320

Please sign in to comment.