There had been several concurrent solutions of Circuit Breaker for PHP on Github but there is no Circuit Breaker for dynamic calling of callback with own responsibility for handling failures. This library was developed especially for easy of use that developer whose uses this library does not have to take interest how it works inside.
Generally The library is for handling failures of the call of the callback, which may be e.g. call of another service.
Calls the callback and if it fails it tries in certain seconds or in certain attempts which is set in constructor.
Contains only two states OPEN (if the callback raises an exception) or CLOSE (if the callback returns data).
- PHP v7.0.0^ (It may work in lower version but it has not been tested yet.)
- Add the lines below to your composer.json file
"repositories": [
{
"type": "vcs",
"url": "git@github.com:heureka/php-circuit-breaker.git"
},
],
"require": {
"heureka/php-circuit-breaker": "*"
}
]
- Run this command
$ composer install
$stopAttemptTime = 10; // How long it should wait for a next try. In seconds.
$stopAttemptCount = 200; // How many attempts it should wait for a next try.
$dataStorage = new \CircuitBreaker\Storage\DataStorage(
new \CircuitBreaker\Storage\ApcuCacheStorageAdapter(), // APC extension must be installed.
'serviceName' // Just for definition of key in the data storage (APC cache key).
);
$circuitBreaker = new \CircuitBreaker\CircuitBreaker($dataStorage, $stopAttemptTime, $stopAttemptCount);
$circuitBreaker->call(
function() use($externalService) {
$externalService->callMethod();
}
);