1. Go to this page and download the library: Download stfn/php-circuit-breaker library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
stfn / php-circuit-breaker example snippets
use Stfn\CircuitBreaker\CircuitBreaker;
$result = CircuitBreaker::for('3rd-party-service')->call(function () {
// Your function that could fail
});
use Stfn\CircuitBreaker\CircuitBreaker;
$breaker = CircuitBreaker::for('3rd-party-service')->forceOpenCircuit();
use Stfn\CircuitBreaker\Storage\RedisStorage;
use Stfn\CircuitBreaker\CircuitBreaker;
$redis = new \Redis();
$redis->connect("127.0.0.1");
$storage = new RedisStorage($redis);
$result = CircuitBreaker::for('3rd-party-service')
->storage($storage)
->call(function () {
// Your function that could fail
});
use Stfn\CircuitBreaker\CircuitBreaker;
$breaker = CircuitBreaker::for('3rd-party-service')
->withOptions([
'failure_threshold' => 10, // Number of failures triggering the transition to the open state
'recovery_time' => 120, // Time in seconds to keep the circuit breaker open before attempting recovery
'sample_duration' => 60, // Duration in seconds within which failures are counted
'consecutive_success' => 3 // Number of consecutive successful calls
use Stfn\CircuitBreaker\CircuitBreakerListener;
use Stfn\CircuitBreaker\CircuitState;
class LoggerListener extends CircuitBreakerListener
{
public function beforeCall(CircuitBreaker $breaker, \Closure $action,...$args) : void
{
Log::info("before call");
}
public function onSuccess(CircuitBreaker $breaker, $result): void
{
Log::info($result);
}
public function onFail(CircuitBreaker $breaker, $exception) : void
{
Log::info($exception);
}
public function onStateChange(CircuitBreaker $breaker, CircuitState $previousState, CircuitState $newState)
{
Log::info($previousState, $newState);
}
}
use Stfn\CircuitBreaker\CircuitBreaker;
$breaker = CircuitBreaker::for('3rd-party-service')
->listeners([new LoggerListener()]);
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.