PHP code example of endisha / resilient-client

1. Go to this page and download the library: Download endisha/resilient-client 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/ */

    

endisha / resilient-client example snippets




use ResilientClient\CircuitBreaker;
use ResilientClient\CircuitBreaker\Adapters\FileStorageAdapter;

$adapter = new FileStorageAdapter;
$adapter->setPath(__DIR__ . '/storage/');

$circuitBreaker = new CircuitBreaker($adapter);
// The maximum number of consecutive failures that can occur before the circuit breaker trips and enters a [failed] state
$circuitBreaker->setFailureThreshold(3);
// The duration that the circuit breaker waits in an [open] state before allowing test requests to determine if the service has recovered (In seconds)
$circuitBreaker->setResetTimeout(300);

$circuitBreaker = new CircuitBreaker($adapter, 'github-cb');

use ResilientClient\CircuitBreaker\Adapters\FileStorageAdapter;
$adapter = new FileStorageAdapter;
$adapter->setPath('path/to/storage');

use ResilientClient\CircuitBreaker\Adapters\PDODatabaseAdapter;
use PDO;

// PDO connection
$pdo = new PDO("mysql:host=localhost;dbname=name", 'user', 'pass');
// Use PDODatabaseAdapter
$adapter = new PDODatabaseAdapter($pdo);
$adapter->setTable('circuit_breaker'); //table: circuit_breaker

$circuit->isNotAvaiable();

$circuit->success();

$circuit->failure();

use ResilientClient\CircuitBreaker\Adapters\FileStorageAdapter;
use ResilientClient\CircuitBreaker\CircuitBreaker;
use ResilientClient\CircuitBreaker\Exceptions\CircuitBreakerException;

$adapter = new FileStorageAdapter;
$adapter->setPath(__DIR__ . '/storage/');

$circuitBreaker = new CircuitBreaker($adapter);
$circuitBreaker->setFailureThreshold(3);
$circuitBreaker->setResetTimeout(300);

try {

    if ($circuitBreaker->isNotAvailable()) {
        throw new CircuitBreakerException('Service is unreachable');
    }

    // Send request
    // ....
    $successRequest = true;
    // ....

    if ($successRequest) {
        $circuitBreaker->success();
    } else {
        $circuitBreaker->failure();
    }
} catch (\CircuitBreakerException $e) {
    echo $e->getMessage();
}

use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use ResilientClient\CircuitBreaker\Middleware\CircuitBreakerGuzzleMiddleware;

$stack = HandlerStack::create(new CurlHandler());
$middleware = new CircuitBreakerGuzzleMiddleware($circuitBreaker);
$stack->push($middleware);

$args['handler'] = $stack;
$client = new Client($args);
// etc..

use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use ResilientClient\CircuitBreaker\Middleware\CircuitBreakerGuzzleMiddleware;

$stack = HandlerStack::create(new CurlHandler());
$middleware = new CircuitBreakerGuzzleMiddleware($circuitBreaker);
$middleware->setAsFailureRequestCodes(301, 302);
$stack->push($middleware);

$args['handler'] = $stack;
$client = new Client($args);
// etc..

use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use ResilientClient\CircuitBreaker\Adapters\FileStorageAdapter;
use ResilientClient\CircuitBreaker\CircuitBreaker;
use ResilientClient\CircuitBreaker\Middleware\CircuitBreakerGuzzleMiddleware;

$adapter = new FileStorageAdapter;
$adapter->setPath(__DIR__ . '/storage/');

$circuitBreaker = new CircuitBreaker($adapter);
$circuitBreaker->setFailureThreshold(3);
$circuitBreaker->setResetTimeout(300);

$stack = HandlerStack::create(new CurlHandler());

$middleware = new CircuitBreakerGuzzleMiddleware($circuitBreaker);
$middleware->setAsFailureRequestCodes(301, 302);
$stack->push($middleware);

$args['handler'] = $stack;
$client = new Client($args);
// etc..