diff --git a/README.md b/README.md index 27f5680..1accd0c 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,62 @@ $result = CircuitBreaker::for('3rd-party-service')->call(function () { }); ``` +### Storage + +By default circuit breaker will use `InMemoryStorage` as a storage driver, which is not so useful if your app is stateless. + +More useful would be to use `RedisStorage`. +```php +use Stfn\CircuitBreaker\Storage\RedisStorage; + +$redis = new \Redis(); +$redis->connect("127.0.0.1"); + +$storage = new RedisStorage($redis); + +$breaker = CircuitBreaker::for('3rd-party-service') + ->storage($storage) + ->call(function () { + // Your function that could fail + }); +``` + +### Config + +Every circuit breaker has its own default config. You can always change it to fit your needs. +```php +$result = CircuitBreaker::for('3rd-party-service') + ->withOptions([ + 'failure_threshold' => 10, + 'recovery_time' => 120 + ]) + ->call(function () { + // Your function that could fail + }); +``` + +### Middlewares + +You can set circuit breaker to fail even if function call didn't throw an exception. + +```php +$breaker = CircuitBreaker::for('test-service') + ->failWhen(function ($result) { + return $result->status() > 400; + }); +``` + +Or you want to avoid some type of failures. + +```php +$breaker = CircuitBreaker::for('test-service') + ->skipFailure(function ($exception) { + return $exception instanceof HttpException; + }); +``` + +### Listeners + ## Testing ```bash diff --git a/src/Storage/RedisStorage.php b/src/Storage/RedisStorage.php index e14edf2..e56c51e 100644 --- a/src/Storage/RedisStorage.php +++ b/src/Storage/RedisStorage.php @@ -23,6 +23,10 @@ class RedisStorage extends CircuitBreakerStorage */ public function __construct(\Redis $redis) { + if (!extension_loaded('redis')) { + throw new \Exception("PHP Redis extension must be loaded."); + } + $this->redis = $redis; }