Download the PHP package algoyounes/circuit-breaker without Composer
On this page you can find all versions of the php package algoyounes/circuit-breaker. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download algoyounes/circuit-breaker
More information about algoyounes/circuit-breaker
Files in algoyounes/circuit-breaker
Package circuit-breaker
Short Description Circuit Breaker is laravel package that provides a way to handle failures gracefully
License MIT
Informations about the package circuit-breaker
Circuit Breaker is a Laravel package that provides a simple and efficient way to manage service calls and prevent cascading failures. It lets you define custom callbacks for key circuit states and run operations with circuit breaker logic.
The following diagram illustrates how the Circuit Breaker Pattern works:
For more info, check the official pattern doc here.
Table of Contents
- Prerequisites
- Installation
- Usage
- Custom Callbacks
- Running an Operation
- Guzzle Middleware Integration
- Laravel Http Facade Integration
- How It Works
- State Transitions
- Half-Open State Behavior
- Configuration
- Contributing
- License
Prerequisites
This package requires:
- PHP 8.2+
- Laravel 11+
- A configured Laravel cache driver
Installation
You can install the package via Composer:
You can publish the configuration file using the following command:
Usage
You can manage specific services with granular control using the forService(...) method. the service-name parameter is a unique identifier key for your service, ensuring its circuit breaker configuration is isolated from other services.
[!TIP] Use the unique
service-namekey across your application to consistently reference the same circuit configuration (e.g., 'payment-service', ...)
Custom Callbacks
You can define callbacks for key circuit states:
| Callback | Description | Parameters Received |
|---|---|---|
onOpen |
Triggered when the circuit goes into OPEN, blocking calls to prevent further failures | CircuitTransition |
onHalfOpen |
The circuit enters HALF-OPEN to test stability, letting one request through | CircuitTransition |
onClose |
The circuit returns to CLOSED, allowing all requests without restrictions | CircuitTransition |
onSuccess |
Fires when a request succeeds, indicating system availability | CircuitResult, CircuitTransition |
onFailure |
Triggered when a request fails, potentially opening the circuit | CircuitResult, CircuitTransition |
onSteadyState |
Indicates the circuit is stable, with no need for changes | CircuitTransition |
Example of defining callbacks:
Running an Operation
To run an operation and manage its state through the circuit breaker, use the run method:
This will execute the provided closure, applying the circuit breaker logic (e.g., open, half-open, closed states) around the service call.
[!NOTE] If you prefer a more direct approach, you can create a
CircuitBuilderinstance:
Simplified Operation
For a simplified approach, use the run method directly from CircuitManager:
Guzzle Middleware Integration
The package provides a Guzzle middleware to automatically manage circuit breaker logic for HTTP requests.
To enable the middleware, add the following to your Guzzle client configuration:
Laravel Http Facade Integration
The package integrates with Laravel's built-in Http facade out of the box:
This automatically applies the circuit breaker middleware to the request using payment-service as the circuit key. You can chain it with any Http method:
When the circuit is open, a RejectedException is thrown:
How It Works
State Transitions
The circuit breaker operates in three states:
- CLOSED — Normal operation. All requests pass through. Failures are counted.
- OPEN — The service is considered down. All requests are rejected immediately without calling the service. After the
cooldown_periodexpires, the circuit moves to HALF-OPEN. - HALF-OPEN — A single probe request is sent to test the service. If it succeeds, the circuit closes. If it fails, the circuit re-opens.
Half-Open State Behavior
When a circuit transitions from OPEN to HALF-OPEN, this package uses a single-probe approach — only one request is allowed to test the recovering service at a time. All other concurrent requests are rejected immediately until the probe completes.
- If the probe succeeds, the circuit closes and normal traffic resumes.
- If the probe fails, the circuit re-opens and a new cooldown period begins.
What your code receives when a request is rejected:
- Via
run()— returns aCircuitResultwhereisSuccess()andisAvailable()both returnfalse - Via
Http::withCircuitBreaker()or Guzzle middleware — throwsRejectedException
Configuration
After publishing the config file, you can adjust these settings in config/circuit-breaker.php:
| Option | Default | Description |
|---|---|---|
enabled |
true |
Enable or disable the circuit breaker globally |
defaults.failure_threshold |
5 |
Number of failures before the circuit opens |
defaults.cooldown_period |
60 |
Seconds to wait before transitioning from OPEN to HALF-OPEN |
defaults.success_threshold |
1 |
Successful probes needed in HALF-OPEN to close the circuit |
cache.ttl |
86400 |
Cache entry lifetime in seconds |
cache.prefix |
circuit-breaker |
Prefix for cache keys |
cache.store |
default |
Laravel cache store to use |
You can also override settings per service:
Contributing
Thank you for considering contributing to the Circuit Breaker package! Please check the CONTRIBUTING file for more details.
License
The Circuit Breaker package is open-sourced software licensed under the MIT license.
All versions of circuit-breaker with dependencies
illuminate/support Version ^11.0|^12.0
illuminate/contracts Version ^11.0|^12.0
illuminate/cache Version ^11.0|^12.0
illuminate/http Version ^11.0|^12.0