Skip to content

A simple implementation of circuit breaker pattern for laravel

License

Notifications You must be signed in to change notification settings

market-muscles-llc/laravel-circuit-breaker

 
 

Repository files navigation

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.

About

A simple implementation of circuit breaker pattern for laravel

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%