Download the PHP package ahmednour/circuit-breaker without Composer
On this page you can find all versions of the php package ahmednour/circuit-breaker. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ahmednour/circuit-breaker
More information about ahmednour/circuit-breaker
Files in ahmednour/circuit-breaker
Package circuit-breaker
Short Description Distributed circuit breaker for Laravel: Redis-backed shared state, single-probe half-open locking, and configurable behaviour when the breaker's own infrastructure fails.
License MIT
Homepage https://github.com/ahmed-nour-dev/circuit-breaker
Informations about the package circuit-breaker
Distributed Circuit Breaker for PHP & Laravel
A production-ready, high-performance distributed Circuit Breaker package for PHP 8.2+ and Laravel. Built with clean architecture principles to protect your applications from cascading failures when third-party APIs (SMS gateways, payment processors, CRMs, email providers, etc.) become degraded or unavailable.
Key Features
- Distributed State Management: Shared Redis circuit state across multiple app instances and background workers.
- Distributed Probe Locking: Ensures exactly one worker probes the external service in
HALF_OPENstate, preventing retry stampedes. - Intelligent HTTP Failure Classification: Differentiates client errors (
4xxvalidation/bad requests) from infrastructure/server outages (5xx, timeouts, connection resets). - Self-Infrastructure Failure Resilience: Storage-agnostic handling for its own infrastructure failures (e.g. Redis outages) supporting Fail-Open, Fail-Closed, and Fallback strategies.
- Octane & Horizon Ready: Seamless connection lifecycle management to prevent stale Redis connections across long-running workers.
- Strict Separation of Concerns: Pure domain logic decoupled from Laravel, Redis, and Guzzle.
- 100% Test Coverage: Complete unit, integration, and infrastructure test suite with in-memory fakes for fast, isolated testing.
Architecture & State Machine
The Four Distinct Failure Types
A resilient circuit breaker must distinguish between four completely different failure scenarios:
| Failure Type | Example | Behavior |
|---|---|---|
| 1. Provider Failure | Twilio returns 503 Service Unavailable or cURL timeout |
Increment failure count; trip to OPEN if threshold is reached. Re-throws provider exception. |
| 2. Circuit Open | Provider is down and recovery timeout has not passed | Fails fast immediately by throwing CircuitOpenException without touching the provider. |
| 3. Circuit Busy | Another worker is currently executing a probe in HALF_OPEN |
Throws CircuitBusyException to avoid concurrent probes against an unhealthy dependency. |
| 4. Infrastructure Failure | Redis server down, connection timeout, network partition | Caught by CircuitBreaker and delegated to the configured policy (fail_open, fail_closed, or fallback). |
Installation
Laravel Service Provider
If package auto-discovery is enabled, CircuitBreakerServiceProvider is loaded automatically. Otherwise, add it to your config/app.php providers array:
Publish the configuration file:
Configuration
The published config file is located at config/circuit-breaker.php:
Usage Examples
1. Basic Execution (Controllers / Services)
Wrap any external service call in the execute method:
2. Laravel Queues & Horizon Integration
Handle circuit open or busy states cleanly in queue jobs with automatic delay releases:
Infrastructure Failure Strategies
If the Circuit Breaker's own storage (e.g. Redis) is down, the package uses the configured infrastructure_failure.strategy:
1. Fail Open (fail_open — Default)
Executes the protected callback anyway. Ideal when service availability is prioritized over protection to prevent a Redis outage from taking down your entire application.
2. Fail Closed (fail_closed)
Rejects the operation and throws CircuitInfrastructureException. Ideal for high-risk operations (e.g. payments, money transfers, hard rate-limited APIs) where executing without safety guarantees is dangerous.
3. Fallback (fallback)
Delegates to an implementation of InfrastructureFallback (e.g. database-backed store or in-memory fallback).
Configure in config/circuit-breaker.php:
HTTP Failure Classification
The default HttpFailureClassifier intelligently distinguishes real provider outages from normal client errors:
- Does NOT Trip (Client Errors):
400 Bad Request,401 Unauthorized,403 Forbidden,404 Not Found,422 Unprocessable Entity- Application bugs and invalid user inputs do not count toward circuit trips.
- Trips Circuit (Server & Network Errors):
500 Internal Server Error,502 Bad Gateway,503 Service Unavailable,504 Gateway Timeout- Guzzle
ConnectExceptionandServerException - Laravel HTTP Client
ConnectionException - Network error messages:
connection timed out,cURL error 28,connection refused,DNS resolution failed,reset by peer
Testing
Run the test suite using PHPUnit:
To run with sail or Docker:
Standalone In-Memory Testing
Domain logic and state machines are fully decoupled and can be tested without Redis using the provided test doubles in Tests\Support:
InMemoryCircuitStoreFakeLockFakeClockAlwaysTripFailureClassifier/NeverTripFailureClassifier
License
This package is open-sourced software licensed under the MIT license.
All versions of circuit-breaker with dependencies
ext-redis Version *
illuminate/contracts Version ^10.0 || ^11.0 || ^12.0 || ^13.0
illuminate/support Version ^10.0 || ^11.0 || ^12.0 || ^13.0