Skip to content

Latest commit

 

History

History
117 lines (79 loc) · 4.12 KB

README.md

File metadata and controls

117 lines (79 loc) · 4.12 KB

🚨 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

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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:

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.