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