PHP code example of idealizetecnologia / circuit-breaker-php

1. Go to this page and download the library: Download idealizetecnologia/circuit-breaker-php 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/ */

    

idealizetecnologia / circuit-breaker-php example snippets


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);

// 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
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 circuit status for service: `my-service`
if (! CircuitBreaker::isAvailable('my-service')) {  
  die('Circuit is not available!');  
}

// Usage example for success and failure  
try {  
  Service::execute('something');  
  CircuitBreaker::success('my-service');  
} catch (\ServiceException $e) {  
  CircuitBreaker::failure('my-service');  
  die($e->getMessage());  
}