ejsmont-artur/php-circuit-breaker-bundle
php-circuit-breaker component is framework-agnostic, but the bundle’s Symfony-specific configurations (e.g., service.xml, Doctrine/Cache integration) limit direct Laravel adoption.Illuminate\Cache). This reduces vendor lock-in if the bundle’s Symfony-specific parts are abstracted.php-circuit-breaker component can be directly integrated into Laravel without the bundle, reducing Symfony dependency. Key classes:
CircuitBreaker (core logic).State (open/half-open/closed states).ExceptionHandler (customizable failure rules).ServiceProvider can replicate the bundle’s service registration.Events or middleware.Cache facade already supports the same backends (Redis, Memcached, etc.).| Risk Area | Mitigation Strategy |
|---|---|
| Symfony 2 Dependency | Use the core php-circuit-breaker component directly; avoid bundle-specific features. |
| State Persistence | Test cache backend compatibility (e.g., Redis driver quirks in Laravel). |
| Thread Safety | Ensure cache locks work in Laravel’s request lifecycle (stateless vs. stateful). |
| Error Handling | Customize ExceptionHandler to align with Laravel’s exception hierarchy. |
| Performance Overhead | Benchmark cache vs. in-memory state storage for high-throughput scenarios. |
| Laravel Component | Bundle Equivalent / Alternative | Compatibility Notes |
|---|---|---|
| Service Container | Symfony’s service.xml → Laravel ServiceProvider |
Register CircuitBreaker as a singleton/bound service. |
| Cache Backend | Doctrine/Cache → Illuminate\Cache |
Supports Redis, Memcached, APCu, database, etc. |
| Event System | Symfony Events → Laravel Events or Middleware |
Use middleware to wrap circuit breaker logic around HTTP requests. |
| Dependency Injection | Symfony DI → Laravel’s bind()/singleton() |
Inject CircuitBreaker into repositories/services via constructor injection. |
| Configuration | config.yml → Laravel config/circuit-breaker.php |
Define thresholds (e.g., failure_threshold, reset_timeout) in config. |
Phase 1: Core Integration (Low Risk)
php-circuit-breaker component via Composer:
composer require ejsmont-artur/php-circuit-breaker
// app/Providers/CircuitBreakerServiceProvider.php
namespace App\Providers;
use Ejsmont\CircuitBreaker\CircuitBreaker;
use Illuminate\Support\ServiceProvider;
class CircuitBreakerServiceProvider extends ServiceProvider {
public function register() {
$this->app->singleton('circuitBreaker', function ($app) {
return new CircuitBreaker(
$app['cache']->driver('redis'), // Cache backend
new \Ejsmont\CircuitBreaker\ExceptionHandler() // Customize if needed
);
});
}
}
Phase 2: Middleware Integration (Medium Risk)
// app/Http/Middleware/CircuitBreakerMiddleware.php
namespace App\Http\Middleware;
use Closure;
use Ejsmont\CircuitBreaker\CircuitBreaker;
class CircuitBreakerMiddleware {
protected $circuitBreaker;
public function __construct(CircuitBreaker $circuitBreaker) {
$this->circuitBreaker = $circuitBreaker;
}
public function handle($request, Closure $next) {
return $this->circuitBreaker->execute('api.payment', function () use ($request, $next) {
return $next($request);
});
}
}
app/Http/Kernel.php:
protected $middleware = [
// ...
\App\Http\Middleware\CircuitBreakerMiddleware::class,
];
Phase 3: Advanced Features (High Risk)
ExceptionHandler to classify Laravel-specific exceptions (e.g., QueryException, HttpException).telescope or Prometheus).config() to override thresholds per environment.reset_timeout/failure_threshold based on real-world data.php-circuit-breaker component is lightweight (low maintenance).config/circuit-breaker.php to avoid hardcoded values.CIRCUIT_BREAKER_RESET_TIMEOUT).tap() or dump() to inspect state during failures:
$this->circuitBreaker->execute('db.migration', function () {
// ...
})->tap(function ($result) {
logger()->debug('Circuit breaker state:', [
'state' => $this->circuitBreaker->getState('db.migration'),
]);
});
How can I help you explore Laravel packages today?