bvtterfly / laravel-circuit-breaker
Laravel circuit breaker package
Fund package maintenance!
bvtterfly
Installs: 7 399
Dependents: 0
Suggesters: 0
Security: 0
Stars: 42
Watchers: 1
Forks: 4
Open Issues: 5
Requires
- php: ^8.0
- illuminate/contracts: ^9.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-03-05 14:36:46 UTC
README
🚨 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.
Laravel Circuit Breaker
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
Installation
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, ];
Usage
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, ]);
Three states of circuit breaker
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(); }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.