1 | <?php |
||
16 | class CircuitBreaker |
||
17 | { |
||
18 | /** |
||
19 | * @var CacheInterface |
||
20 | */ |
||
21 | protected $cache; |
||
22 | |||
23 | /** |
||
24 | * @var LoggerInterface |
||
25 | */ |
||
26 | protected $logger; |
||
27 | |||
28 | /** |
||
29 | * @var Service[] |
||
30 | */ |
||
31 | protected $services = []; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $cachePrefix = 'cb'; |
||
37 | |||
38 | /** |
||
39 | * @var int |
||
40 | */ |
||
41 | protected $ttl = 3360; |
||
42 | |||
43 | /** |
||
44 | * CircuitBreaker constructor. |
||
45 | * |
||
46 | * @param CacheInterface $cache Cache to store the failures and times |
||
47 | * @param LoggerInterface|null $logger To log stuff |
||
48 | */ |
||
49 | 1 | public function __construct( |
|
50 | CacheInterface $cache, |
||
51 | LoggerInterface $logger |
||
52 | ) { |
||
53 | 1 | $this->cache = $cache; |
|
54 | 1 | $this->logger = $logger instanceof LoggerInterface ? $logger : new NullLogger(); |
|
55 | 1 | } |
|
56 | |||
57 | /** |
||
58 | * @param string $serviceName Name of the service |
||
59 | * @param string $field Field you want to fetch |
||
60 | * |
||
61 | * @return string |
||
62 | */ |
||
63 | 1 | protected function getCacheKey($serviceName, $field) |
|
64 | { |
||
65 | 1 | return $this->cachePrefix.'-'.$serviceName.'-'.$field; |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param $serviceName |
||
70 | * @return Service|null |
||
71 | */ |
||
72 | 1 | protected function getService($serviceName) |
|
73 | { |
||
74 | 1 | if (array_key_exists($serviceName, $this->services)) { |
|
75 | 1 | return $this->services[$serviceName]; |
|
76 | } |
||
77 | 1 | return null; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param string $serviceName Name of the service |
||
82 | * |
||
83 | * @return int |
||
84 | */ |
||
85 | 1 | protected function getFailures($serviceName) |
|
86 | { |
||
87 | 1 | return (int) $this->cache->get($this->getCacheKey($serviceName, 'failures')); |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param string $serviceName Name of the service |
||
92 | * |
||
93 | * @return int |
||
94 | */ |
||
95 | 1 | protected function getLastTest($serviceName) |
|
99 | |||
100 | /** |
||
101 | * @param string $serviceName Name of the service |
||
102 | * @param int $newValue New value to store |
||
103 | */ |
||
104 | 1 | protected function setFailures($serviceName, $newValue) |
|
109 | |||
110 | /** |
||
111 | * @param Service $service Service to track |
||
112 | * |
||
113 | * @throws ServiceAlreadyTrackedException |
||
114 | */ |
||
115 | 1 | public function trackService(Service $service) |
|
122 | |||
123 | /** |
||
124 | * @param string $serviceName Name of the service |
||
125 | * |
||
126 | * @return bool |
||
127 | * @throws ServiceNotTrackedException |
||
128 | */ |
||
129 | 1 | public function isAvailable($serviceName) |
|
155 | |||
156 | /** |
||
157 | * @param string $serviceName Name of the service |
||
158 | * |
||
159 | * @throws ServiceNotTrackedException |
||
160 | */ |
||
161 | 1 | public function reportFailure($serviceName) |
|
170 | |||
171 | /** |
||
172 | * @param string $serviceName Name of the service |
||
173 | * |
||
174 | * @throws ServiceNotTrackedException |
||
175 | */ |
||
176 | 1 | public function reportSuccess($serviceName) |
|
193 | } |
||
194 |