-
Notifications
You must be signed in to change notification settings - Fork 25
/
RedisClusterAdapterExample.php
44 lines (35 loc) · 1.13 KB
/
RedisClusterAdapterExample.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
use LeoCarmo\CircuitBreaker\CircuitBreaker;
use LeoCarmo\CircuitBreaker\Adapters\RedisClusterAdapter;
// Connect to redis
$redis = new \Redis();
$redis->connect('localhost', 6379);
$adapter = new RedisClusterAdapter($redis, 'my-product');
// Set redis adapter for CB
$circuit = new CircuitBreaker($adapter, 'my-service');
// Configure settings for CB
$circuit->setSettings([
'timeWindow' => 60, // Time for an open circuit (seconds)
'failureRateThreshold' => 50, // Fail rate for open the circuit
'intervalToHalfOpen' => 30, // Half open time (seconds)
]);
// Check circuit status for service
if (! $circuit->isAvailable()) {
die('Circuit is not available!');
}
// Usage example for success and failure
function myService() {
if (rand(1, 100) >= 50) {
throw new RuntimeException('Something got wrong!');
}
}
try {
myService();
$circuit->success();
echo 'success!' . PHP_EOL;
} catch (RuntimeException $e) {
// If an error occurred, it must be recorded as failure.
$circuit->failure();
echo 'fail!' . PHP_EOL;
}