🚨 THIS PACKAGE HAS BEEN ABANDONED 🚨
I no longer use Laravel and cannot justify the time needed to maintain this package. That's why I have chosen to abandon it. Feel free to fork my code and maintain your own copy.
This package is a simple implementation of circuit breaker pattern for laravel. It protects your application from failures of its service dependencies.
Resources about the circuit breaker pattern:
- http://martinfowler.com/bliki/CircuitBreaker.html
- https://github.com/Netflix/Hystrix/wiki/How-it-Works#CircuitBreaker
You can install the package via composer:
composer require bvtterfly/laravel-circuit-breaker
You can publish the config file with:
php artisan vendor:publish --tag="circuit-breaker-config"
This is the contents of the published config file:
return [
// Here you may specify which of your cache stores you wish to use as your default store.
'store' => config('cache.default'),
// length of interval (in seconds) over which it calculates the error rate
'time_window' => 60,
// the number of errors to encounter within a given timespan before opening the circuit
'error_threshold' => 10,
// the amount of time until the circuit breaker will try to query the resource again
'error_timeout' => 300,
// the timeout for the circuit when it is in the half-open state
'half_open_timeout' => 150,
// the amount of consecutive successes for the circuit to close again
'success_threshold' => 1,
];
Your application may have multiple services, so you will have to get a circuit breaker for each service:
use Bvtterfly\LaravelCircuitBreaker\Facades\CircuitBreaker;
$circuit = CircuitBreaker::service('my-service');
// or you can override default configuration:
$circuit = CircuitBreaker::service('my-service', [
'time_window' => 120,
'success_threshold' => 3,
]);
You can then determine whether a service is available or not.
// Check circuit status for service
if (! $circuit->isAvailable()) {
// Service isn't available
}
Service is available if it's CLOSED or HALF_OPEN. Then, you should call your service, depending on the response. You can mark it as a success or failure to update the circuit status.
try {
callAPI();
$circuit->markSuccess();
} catch (\Exception $e) {
// If an error occurred, it must be recorded as failed.
$circuit->markFailed();
}
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.