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\Adapter\Redis\RedisCircuitBreaker;
use GabrielAnhaia\PhpCircuitBreaker\CircuitBreaker;

$settings = [
    'exceptions_on' => false,
    'time_window' => 20,
    'time_out_open' => 30,
    'time_out_half_open' => 20,
    'total_failures' => 5,
];

$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);

$driver = new RedisCircuitBreaker($redis);
$cb = new CircuitBreaker($driver, $settings);

$service = 'PAYMENTS_API';
if (!$cb->canPass($service)) {
    // Short-circuit
    return;
}

try {
    // Call dependency...
    $cb->succeed($service);
} catch (\Throwable $e) {
    $cb->failed($service);
}

use GabrielAnhaia\PhpCircuitBreaker\Contract\Alert;

class LoggerAlert implements Alert {
    public function emmitOpenCircuit(string $serviceName)
    {
        error_log("Circuit opened: {$serviceName}");
    }
}