PHP code example of gabrielanhaia / php-circuit-breaker
1. Go to this page and download the library: Download gabrielanhaia/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/ */
gabrielanhaia / php-circuit-breaker example snippets
use GabrielAnhaia\PhpCircuitBreaker\CircuitBreaker;
use GabrielAnhaia\PhpCircuitBreaker\CircuitBreakerConfig;
use GabrielAnhaia\PhpCircuitBreaker\Storage\InMemoryStorage;
$storage = new InMemoryStorage();
$config = new CircuitBreakerConfig(
failureThreshold: 5,
openTimeout: 30,
);
$cb = new CircuitBreaker($storage, $config);
$service = 'payment-api';
if (!$cb->canPass($service)) {
// Circuit is open — use fallback
return cachedResponse();
}
try {
$result = callPaymentApi();
$cb->recordSuccess($service);
return $result;
} catch (\Throwable $e) {
$cb->recordFailure($service);
return cachedResponse();
}
bash
composer