1 | <?php |
||
16 | class CircuitBreaker implements CircuitBreakerInterface |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * @var CircuitBreakerInterface used to load/save availability statistics |
||
21 | */ |
||
22 | protected $storageAdapter; |
||
23 | |||
24 | /** |
||
25 | * @var int default threshold, if service fails this many times will be disabled |
||
26 | */ |
||
27 | protected $defaultMaxFailures; |
||
28 | |||
29 | /** |
||
30 | * @var int how many seconds should we wait before retry |
||
31 | */ |
||
32 | protected $defaultRetryTimeout; |
||
33 | |||
34 | /** |
||
35 | * Array with configuration per service name, format: |
||
36 | * array( |
||
37 | * "serviceName1" => array('maxFailures' => X, 'retryTimeout => Y), |
||
38 | * "serviceName2" => array('maxFailures' => X, 'retryTimeout => Y), |
||
39 | * ) |
||
40 | * |
||
41 | * @var array[] settings per service name |
||
42 | */ |
||
43 | protected $settings = array(); |
||
44 | |||
45 | /** |
||
46 | * Configure instance with storage implementation and default threshold and retry timeout. |
||
47 | * |
||
48 | * @param StorageInterface $storage storage implementation |
||
49 | * @param int $maxFailures default threshold, if service fails this many times will be disabled |
||
50 | * @param int $retryTimeout how many seconds should we wait before retry |
||
51 | */ |
||
52 | 11 | public function __construct(StorageInterface $storage, $maxFailures = 20, $retryTimeout = 60) |
|
58 | |||
59 | /** |
||
60 | * Use this method only if you want to add server specific threshold and retry timeout. |
||
61 | * |
||
62 | * @param $serviceName |
||
63 | * @param int $maxFailures default threshold, if service fails this many times will be disabled |
||
64 | * @param int $retryTimeout how many seconds should we wait before retry |
||
65 | * |
||
66 | * @return CircuitBreaker |
||
67 | * @internal param StorageInterface $storage storage implementation |
||
68 | */ |
||
69 | 11 | public function setServiceSettings($serviceName, $maxFailures, $retryTimeout) |
|
78 | |||
79 | // ---------------------- HELPERS ----------------- |
||
80 | |||
81 | 8 | public function isAvailable(string $serviceName): bool |
|
112 | |||
113 | // ---------------------- Directly accessed by interface methods ----------------- |
||
114 | |||
115 | /** |
||
116 | * @param string $serviceName |
||
117 | * |
||
118 | * @return int |
||
119 | */ |
||
120 | 11 | protected function getFailures(string $serviceName) |
|
125 | |||
126 | /** |
||
127 | * @param string $serviceName |
||
128 | * |
||
129 | * @return int |
||
130 | */ |
||
131 | 11 | protected function getMaxFailures(string $serviceName) |
|
135 | |||
136 | /** |
||
137 | * Load setting or initialise service name with defaults for faster lookups |
||
138 | * |
||
139 | * @param string $serviceName what service to look for |
||
140 | * @param string $variable what setting to look for |
||
141 | * |
||
142 | * @return int |
||
143 | */ |
||
144 | 11 | private function getSetting(string $serviceName, string $variable) |
|
155 | |||
156 | /** |
||
157 | * @param string $serviceName |
||
158 | * |
||
159 | * @return int |
||
160 | */ |
||
161 | 7 | protected function getLastTest(string $serviceName) |
|
166 | |||
167 | /** |
||
168 | * @param string $serviceName |
||
169 | * |
||
170 | * @return int |
||
171 | */ |
||
172 | 7 | protected function getRetryTimeout(string $serviceName) |
|
176 | |||
177 | |||
178 | /** |
||
179 | * @param string $serviceName |
||
180 | * @param integer $newValue |
||
181 | */ |
||
182 | 8 | protected function setFailures(string $serviceName, $newValue) |
|
189 | |||
190 | |||
191 | 8 | public function reportFailure(string $serviceName) |
|
196 | |||
197 | |||
198 | 5 | public function reportSuccess(string $serviceName) |
|
216 | |||
217 | } |
||
218 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..