-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2cad6c
commit 2180e69
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |