Skip to content

Commit

Permalink
Add Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
stfndamjanovic committed Jan 18, 2024
1 parent b9ed6b6 commit 84f35ac
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/Storage/RedisStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 84f35ac

Please sign in to comment.