idealizetecnologia / circuit-breaker-php
Circuit Breaker for PHP
2.0.2
2019-10-07 13:04 UTC
Requires
- php: >=7.1
- ext-redis: *
Requires (Dev)
- phpunit/php-code-coverage: ^6.1
- phpunit/phpunit: ~7.0
- symfony/var-dumper: ^4.3
This package is not auto-updated.
Last update: 2025-03-04 17:17:26 UTC
README
For more information about this pattern see this.
This implementation has only redis adapter yet
Starting with composer
composer require leocarmo/circuit-breaker-php
Redis adapter
The first argument is a redis connection, the second is your product name, for redis namespace avoid key conflicts with another product using the same redis.
use LeoCarmo\CircuitBreaker\CircuitBreaker; // Connect to redis $redis = new \Redis(); $redis->connect('localhost', 6379); $adapter = new \LeoCarmo\CircuitBreaker\Adapters\RedisAdapter($redis, 'my-product'); // Set redis adapter for CB CircuitBreaker::setAdapter($adapter);
Set circuit break settings
This is not required, default values will be set
// Configure settings for CB CircuitBreaker::setGlobalSettings([ 'timeWindow' => 60, // Time for an open circuit (seconds) 'failureRateThreshold' => 50, // Fail rate for open the circuit 'intervalToHalfOpen' => 30, // Half open time (seconds) ]);
Configure settings for specific service
// Configure settings for specific service CircuitBreaker::setServiceSettings('my-custom-service', [ 'timeWindow' => 30, // Time for an open circuit (seconds) 'failureRateThreshold' => 15, // Fail rate for open the circuit 'intervalToHalfOpen' => 10, // Half open time (seconds) ]);
Check if circuit is available (closed)
Each check is for a specific service. So you can have multiple services in the same application, and when one circuit is open, the other works normally.
// Check circuit status for service: `my-service` if (! CircuitBreaker::isAvailable('my-service')) { die('Circuit is not available!'); }
Record success and failure
// Usage example for success and failure try { Service::execute('something'); CircuitBreaker::success('my-service'); } catch (\ServiceException $e) { CircuitBreaker::failure('my-service'); die($e->getMessage()); }