-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from celosauro/add_redis-cluster-adapter
Added: RedisClusterAdapter
- Loading branch information
Showing
4 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php declare(strict_types=1); | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use LeoCarmo\CircuitBreaker\CircuitBreaker; | ||
use LeoCarmo\CircuitBreaker\Adapters\RedisClusterAdapter; | ||
|
||
// Connect to redis | ||
$redis = new \Redis(); | ||
$redis->connect('localhost', 6379); | ||
|
||
$adapter = new RedisClusterAdapter($redis, 'my-product'); | ||
|
||
// Set redis adapter for CB | ||
$circuit = new CircuitBreaker($adapter, 'my-service'); | ||
|
||
// Configure settings for CB | ||
$circuit->setSettings([ | ||
'timeWindow' => 60, // Time for an open circuit (seconds) | ||
'failureRateThreshold' => 50, // Fail rate for open the circuit | ||
'intervalToHalfOpen' => 30, // Half open time (seconds) | ||
]); | ||
|
||
// Check circuit status for service | ||
if (! $circuit->isAvailable()) { | ||
die('Circuit is not available!'); | ||
} | ||
|
||
// Usage example for success and failure | ||
function myService() { | ||
if (rand(1, 100) >= 50) { | ||
throw new RuntimeException('Something got wrong!'); | ||
} | ||
} | ||
|
||
try { | ||
myService(); | ||
$circuit->success(); | ||
echo 'success!' . PHP_EOL; | ||
} catch (RuntimeException $e) { | ||
// If an error occurred, it must be recorded as failure. | ||
$circuit->failure(); | ||
echo 'fail!' . PHP_EOL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace LeoCarmo\CircuitBreaker\Adapters; | ||
|
||
class RedisClusterAdapter implements AdapterInterface | ||
{ | ||
/** | ||
* @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 | ||
* @return bool | ||
*/ | ||
public function incrementFailure(string $service, int $timeWindow): bool | ||
{ | ||
$serviceName = $this->makeNamespace($service) . ':failures'; | ||
|
||
if (! $this->redis->get($serviceName)) { | ||
$this->redis->incr($serviceName); | ||
return (bool) $this->redis->expire($serviceName, $timeWindow); | ||
} | ||
|
||
return (bool) $this->redis->incr($serviceName); | ||
} | ||
|
||
/** | ||
* @param string $service | ||
*/ | ||
public function setSuccess(string $service): void | ||
{ | ||
$serviceName = $this->makeNamespace($service); | ||
|
||
$this->redis->del($serviceName . ':open'); | ||
$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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters