1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace CircuitBreakerBundle\Service; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use Psr\Log\LoggerTrait as PsrLoggerTrait; |
7
|
|
|
use Symfony\Component\Cache\Adapter\AdapterInterface; |
8
|
|
|
|
9
|
|
|
class CircuitBreakerService |
10
|
|
|
{ |
11
|
|
|
use PsrLoggerTrait; |
12
|
|
|
|
13
|
|
|
const OPEN = 'open'; |
14
|
|
|
const CLOSED = 'closed'; |
15
|
|
|
const HALFOPEN = 'half-open'; |
16
|
|
|
|
17
|
|
|
const STATUS_UP = true; |
18
|
|
|
const STATUS_DOWN = false; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var AdapterInterface |
22
|
|
|
*/ |
23
|
|
|
private $cacheApp; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $status; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
private $threshold; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
private $timeout; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param AdapterInterface $cacheApp |
42
|
|
|
* @param int $threshold |
43
|
|
|
* @param int $timeout |
44
|
|
|
*/ |
45
|
11 |
|
public function __construct(AdapterInterface $cacheApp, int $threshold, int $timeout) |
46
|
|
|
{ |
47
|
11 |
|
$this->cacheApp = $cacheApp; |
48
|
11 |
|
$this->threshold = $threshold; |
49
|
11 |
|
$this->timeout = $timeout; |
50
|
11 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $key The service key |
54
|
|
|
* @param bool $status The service status (true: up, false: down) |
55
|
|
|
*/ |
56
|
9 |
|
public function save(string $key, bool $status) |
57
|
|
|
{ |
58
|
9 |
|
if (!isset($this->status[$key])) { |
59
|
9 |
|
$this->status[$key] = self::CLOSED; |
60
|
|
|
} |
61
|
|
|
|
62
|
9 |
|
if ($this->status[$key] === self::OPEN) { |
63
|
2 |
|
$this->attemptReset($key); |
64
|
|
|
} |
65
|
|
|
|
66
|
9 |
|
if (!$status) { |
67
|
7 |
|
$this->countFailure($key); |
68
|
|
|
} else { |
69
|
4 |
|
$this->resetCount($key); |
70
|
|
|
} |
71
|
9 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Verify if service is open. |
75
|
|
|
* |
76
|
|
|
* @param string $service |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
11 |
|
public function isOpen(string $service): bool |
81
|
|
|
{ |
82
|
11 |
|
if (!isset($this->status[$service]) && |
83
|
2 |
|
$this->cacheApp->getItem($service)->get() >= $this->threshold |
84
|
|
|
) { |
85
|
1 |
|
$this->status[$service] = self::OPEN; |
86
|
|
|
} |
87
|
|
|
|
88
|
11 |
|
return isset($this->status[$service]) && $this->status[$service] === self::OPEN; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Increment number of fail to one service |
93
|
|
|
* |
94
|
|
|
* @param string $service |
95
|
|
|
*/ |
96
|
7 |
|
private function countFailure(string $service) |
97
|
|
|
{ |
98
|
7 |
|
$this->notice('[CircuitBreaker] call countFailure to ' . $service); |
99
|
7 |
|
$value = $this->cacheApp->getItem($service); |
100
|
7 |
|
$fail = $value->get() + 1; |
101
|
7 |
|
$value->set($fail); |
102
|
|
|
|
103
|
7 |
|
if ($this->status[$service] === self::HALFOPEN) { |
104
|
1 |
|
$value->set($this->threshold); |
105
|
|
|
} |
106
|
|
|
|
107
|
7 |
|
$value->expiresAfter($this->timeout); |
108
|
|
|
|
109
|
7 |
|
if ($fail >= $this->threshold) { |
110
|
4 |
|
$this->tripBreaker($service); |
111
|
|
|
} |
112
|
|
|
|
113
|
7 |
|
$this->cacheApp->save($value); |
114
|
7 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Open circuit breaker. |
118
|
|
|
* |
119
|
|
|
* @param string $service |
120
|
|
|
*/ |
121
|
4 |
|
private function tripBreaker(string $service) |
122
|
|
|
{ |
123
|
4 |
|
$this->error('[CircuitBreaker] call tripBreaker to ' . $service); |
124
|
4 |
|
$this->status[$service] = self::OPEN; |
125
|
4 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* CLose circuit breaker, and reset value to fail service. |
129
|
|
|
* |
130
|
|
|
* @param string $service |
131
|
|
|
*/ |
132
|
4 |
|
private function resetCount(string $service) |
133
|
|
|
{ |
134
|
4 |
|
$this->info('[CircuitBreaker] call resetCount to ' . $service); |
135
|
4 |
|
$value = $this->cacheApp->getItem($service); |
136
|
|
|
|
137
|
4 |
|
$value->set(0); |
138
|
4 |
|
$this->status[$service] = self::CLOSED; |
139
|
4 |
|
$this->cacheApp->save($value); |
140
|
4 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* HalfOpen circuit breaker. |
144
|
|
|
* |
145
|
|
|
* @param string $service |
146
|
|
|
*/ |
147
|
2 |
|
private function attemptReset(string $service) |
148
|
|
|
{ |
149
|
2 |
|
$this->warning('[CircuitBreaker] call attemptReset to ' . $service); |
150
|
2 |
|
$this->status[$service] = self::HALFOPEN; |
151
|
2 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @var LoggerInterface |
155
|
|
|
*/ |
156
|
|
|
private $logger; |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param LoggerInterface $logger |
160
|
|
|
*/ |
161
|
10 |
|
public function setLogger(LoggerInterface $logger) |
162
|
|
|
{ |
163
|
10 |
|
$this->logger = $logger; |
164
|
10 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $level |
168
|
|
|
* @param string $message |
169
|
|
|
* @param array $context |
170
|
|
|
*/ |
171
|
9 |
|
public function log($level, $message, array $context = array()) |
172
|
|
|
{ |
173
|
9 |
|
$this->logger->log($level, $message, $context); |
174
|
9 |
|
} |
175
|
|
|
} |
176
|
|
|
|