Skip to content

Commit

Permalink
feature: Implement example
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielanhaia committed Jun 24, 2020
1 parent a2cad6c commit 2180e69
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions example/index.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
<?php

use GabrielAnhaia\PhpCircuitBreaker\Adapter\Redis\RedisCircuitBreaker;
use GabrielAnhaia\PhpCircuitBreaker\CircuitBreaker;

require_once('../vendor/autoload.php');
require_once('ServiceExample.php');

// You can insert this part in your service container to inject the dependencies.
$settings = [
'exceptions_on' => false,
'time_window' => 10,
'time_out_open' => 10,
'time_out_half_open' => 10,
'total_failures' => 5
];
$redis = new \Redis;
$redis->connect('localhost');
$redisCircuitBreaker = new RedisCircuitBreaker($redis);
$circuitBreaker = new CircuitBreaker($redisCircuitBreaker, $settings);

$serviceName = 'MICROSERVICE_NAME';

if ($circuitBreaker->canPass($serviceName) !== true) {
return;
}

// First attempt example (success).
try {
// You can change the parameter to below to false and run this code 5 times in less than the "time_window",
// If you do that, it will open the circuit and the method "canPass" will return false until the time_out_open runs out.
ServiceExample::callServiceExample(false);
$circuitBreaker->succeed($serviceName);
print_r('SUCCESS');
} catch (\Exception $exception) {
$circuitBreaker->failed($serviceName);
print_r('FAIL');
return;
}

0 comments on commit 2180e69

Please sign in to comment.