-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathcommon.php
73 lines (63 loc) · 2.05 KB
/
common.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
use \Ackintosh\Ganesha;
use \Ackintosh\Ganesha\Builder;
require_once dirname(__DIR__) . '/vendor/autoload.php';
define('SERVICE', 'example');
define('TIME_WINDOW', 20);
define('FAILURE_RATE', 10);
define('MINIMUM_REQUESTS', 10);
define('INTERVAL_TO_HALF_OPEN', 5);
define('SERVER_STATE_DATA', __DIR__ . '/server/state.dat');
define('SERVER_STATE_NORMAL', 'normal');
define('SERVER_STATE_ABNORMAL', 'abnormal');
function buildGanesha($storage)
{
switch ($storage) {
case 'apcu':
$adapter = new Ackintosh\Ganesha\Storage\Adapter\Apcu();
break;
case 'redis':
$redis = new \Redis();
$redis->connect(getenv('GANESHA_EXAMPLE_REDIS') ?: 'localhost');
$adapter = new Ackintosh\Ganesha\Storage\Adapter\Redis($redis);
break;
case 'memcached':
$m = new \Memcached();
$m->addServer(getenv('GANESHA_EXAMPLE_MEMCACHED') ?: 'localhost', 11211);
$adapter = new \Ackintosh\Ganesha\Storage\Adapter\Memcached($m);
break;
default:
throw new \InvalidArgumentException();
break;
}
$ganesha = Builder::withRateStrategy()
->adapter($adapter)
->timeWindow(TIME_WINDOW)
->failureRateThreshold(FAILURE_RATE)
->minimumRequests(MINIMUM_REQUESTS)
->intervalToHalfOpen(INTERVAL_TO_HALF_OPEN)
->build();
$messageOnTripped = <<<__EOS__
!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!! TRIPPED !!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!
__EOS__;
$messageOnCalmedDown = <<<__EOS__
=======================
===== CALMED DOWN =====
=======================
__EOS__;
$ganesha->subscribe(function ($event, $service, $message) use ($messageOnTripped, $messageOnCalmedDown) {
switch ($event) {
case Ganesha::EVENT_TRIPPED:
echo $messageOnTripped;
break;
case Ganesha::EVENT_CALMED_DOWN:
echo $messageOnCalmedDown;
break;
default:
break;
}
});
return $ganesha;
}