1
|
|
|
<?php |
2
|
|
|
namespace Ackintosh\Ganesha; |
3
|
|
|
|
4
|
|
|
use Ackintosh\Ganesha\Exception\StorageException; |
5
|
|
|
use Ackintosh\Ganesha\Storage\Adapter\TumblingTimeWindowInterface; |
6
|
|
|
use Ackintosh\Ganesha\Storage\Adapter\SlidingTimeWindowInterface; |
7
|
|
|
use Ackintosh\Ganesha\Storage\AdapterInterface; |
8
|
|
|
use Ackintosh\Ganesha\Storage\StorageKeys; |
9
|
|
|
use Ackintosh\Ganesha\Storage\StorageKeysInterface; |
10
|
|
|
|
11
|
|
|
class Storage |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Storage\AdapterInterface |
15
|
|
|
*/ |
16
|
|
|
private $adapter; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var callable|null |
20
|
|
|
*/ |
21
|
|
|
private $serviceNameDecorator; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var StorageKeysInterface |
25
|
|
|
*/ |
26
|
|
|
private $storageKeys; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Storage constructor. |
30
|
|
|
* |
31
|
|
|
* @param AdapterInterface $adapter |
32
|
|
|
* @param StorageKeysInterface $storageKeys |
33
|
|
|
* @param callable|null $serviceNameDecorator |
34
|
|
|
*/ |
35
|
|
|
public function __construct( |
36
|
|
|
AdapterInterface $adapter, |
37
|
|
|
StorageKeysInterface $storageKeys, |
38
|
|
|
callable $serviceNameDecorator = null |
39
|
|
|
) { |
40
|
|
|
$this->adapter = $adapter; |
41
|
|
|
$this->serviceNameDecorator = $serviceNameDecorator; |
42
|
|
|
$this->storageKeys = $storageKeys; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* returns count |
47
|
|
|
* |
48
|
|
|
* @param string $key |
49
|
|
|
* @return int |
50
|
|
|
* @throws StorageException |
51
|
|
|
*/ |
52
|
|
|
private function getCount(string $key): int |
53
|
|
|
{ |
54
|
|
|
return $this->adapter->load($key); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* returns success count |
59
|
|
|
* |
60
|
|
|
* @param string $key |
61
|
|
|
* @return int |
62
|
|
|
* @throws StorageException |
63
|
|
|
*/ |
64
|
|
|
public function getSuccessCountByCustomKey(string $key): int |
65
|
|
|
{ |
66
|
|
|
return $this->getCount($this->prefix($key) . $this->storageKeys->success()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* returns failure count |
71
|
|
|
* |
72
|
|
|
* @param string $key |
73
|
|
|
* @return int |
74
|
|
|
* @throws StorageException |
75
|
|
|
*/ |
76
|
|
|
public function getFailureCountByCustomKey(string $key): int |
77
|
|
|
{ |
78
|
|
|
return $this->getCount($this->prefix($key) . $this->storageKeys->failure()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* returns rejection count |
83
|
|
|
* |
84
|
|
|
* @param string $key |
85
|
|
|
* @return int |
86
|
|
|
* @throws StorageException |
87
|
|
|
*/ |
88
|
|
|
public function getRejectionCountByCustomKey(string $key): int |
89
|
|
|
{ |
90
|
|
|
return $this->getCount($this->prefix($key) . $this->storageKeys->rejection()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* returns failure count |
95
|
|
|
* |
96
|
|
|
* @param string $service |
97
|
|
|
* @return int |
98
|
|
|
* @throws StorageException |
99
|
|
|
*/ |
100
|
|
|
public function getFailureCount(string $service): int |
101
|
|
|
{ |
102
|
|
|
return $this->getCount($this->failureKey($service)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* returns success count |
107
|
|
|
* |
108
|
|
|
* @param string $service |
109
|
|
|
* @return int |
110
|
|
|
* @throws StorageException |
111
|
|
|
*/ |
112
|
|
|
public function getSuccessCount(string $service): int |
113
|
|
|
{ |
114
|
|
|
return $this->getCount($this->successKey($service)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* increments failure count |
119
|
|
|
* |
120
|
|
|
* @param string $service |
121
|
|
|
* @return void |
122
|
|
|
* @throws StorageException |
123
|
|
|
*/ |
124
|
|
|
public function incrementFailureCount(string $service): void |
125
|
|
|
{ |
126
|
|
|
$this->adapter->increment($this->failureKey($service)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* decrements failure count |
131
|
|
|
* |
132
|
|
|
* @param string $service |
133
|
|
|
* @return void |
134
|
|
|
* @throws StorageException |
135
|
|
|
*/ |
136
|
|
|
public function decrementFailureCount(string $service): void |
137
|
|
|
{ |
138
|
|
|
$this->adapter->decrement($this->failureKey($service)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* increments success count |
143
|
|
|
* |
144
|
|
|
* @param string $service |
145
|
|
|
* @return void |
146
|
|
|
* @throws StorageException |
147
|
|
|
*/ |
148
|
|
|
public function incrementSuccessCount(string $service): void |
149
|
|
|
{ |
150
|
|
|
$this->adapter->increment($this->successKey($service)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* returns rejection count |
155
|
|
|
* |
156
|
|
|
* @param string $service |
157
|
|
|
* @return int |
158
|
|
|
* @throws StorageException |
159
|
|
|
*/ |
160
|
|
|
public function getRejectionCount(string $service): int |
161
|
|
|
{ |
162
|
|
|
return $this->getCount($this->rejectionKey($service)); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* increments rejection count |
167
|
|
|
* |
168
|
|
|
* @param string $service |
169
|
|
|
* @return void |
170
|
|
|
* @throws StorageException |
171
|
|
|
*/ |
172
|
|
|
public function incrementRejectionCount(string $service): void |
173
|
|
|
{ |
174
|
|
|
$this->adapter->increment($this->rejectionKey($service)); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* sets failure count |
179
|
|
|
* |
180
|
|
|
* @param $service |
181
|
|
|
* @param $failureCount |
182
|
|
|
* @throws StorageException |
183
|
|
|
*/ |
184
|
|
|
public function setFailureCount(string $service, int $failureCount): void |
185
|
|
|
{ |
186
|
|
|
$this->adapter->save($this->failureKey($service), $failureCount); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* sets last failure time |
191
|
|
|
* |
192
|
|
|
* @param string $service |
193
|
|
|
* @param int $lastFailureTime |
194
|
|
|
* @return void |
195
|
|
|
* @throws StorageException |
196
|
|
|
*/ |
197
|
|
|
public function setLastFailureTime(string $service, int $lastFailureTime): void |
198
|
|
|
{ |
199
|
|
|
$this->adapter->saveLastFailureTime($this->lastFailureKey($service), $lastFailureTime); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* returns last failure time |
204
|
|
|
* |
205
|
|
|
* @param string $service |
206
|
|
|
* @return int | null |
207
|
|
|
* @throws StorageException |
208
|
|
|
*/ |
209
|
|
|
public function getLastFailureTime(string $service) |
210
|
|
|
{ |
211
|
|
|
return $this->adapter->loadLastFailureTime($this->lastFailureKey($service)); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* sets status |
216
|
|
|
* |
217
|
|
|
* @param string $service |
218
|
|
|
* @param int $status |
219
|
|
|
* @return void |
220
|
|
|
* @throws StorageException |
221
|
|
|
*/ |
222
|
|
|
public function setStatus(string $service, int $status): void |
223
|
|
|
{ |
224
|
|
|
$this->adapter->saveStatus($this->statusKey($service), $status); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* returns status |
229
|
|
|
* |
230
|
|
|
* @param string $service |
231
|
|
|
* @return int |
232
|
|
|
* @throws StorageException |
233
|
|
|
*/ |
234
|
|
|
public function getStatus(string $service): int |
235
|
|
|
{ |
236
|
|
|
return $this->adapter->loadStatus($this->statusKey($service)); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @return void |
241
|
|
|
*/ |
242
|
|
|
public function reset(): void |
243
|
|
|
{ |
244
|
|
|
$this->adapter->reset(); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return bool |
249
|
|
|
*/ |
250
|
|
|
public function supportTumblingTimeWindow(): bool |
251
|
|
|
{ |
252
|
|
|
return $this->adapter instanceof TumblingTimeWindowInterface; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @return bool |
257
|
|
|
*/ |
258
|
|
|
public function supportSlidingTimeWindow(): bool |
259
|
|
|
{ |
260
|
|
|
return $this->adapter instanceof SlidingTimeWindowInterface; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @param string $service |
265
|
|
|
* @return string |
266
|
|
|
*/ |
267
|
|
|
private function key(string $service): string |
268
|
|
|
{ |
269
|
|
|
if ($this->serviceNameDecorator) { |
270
|
|
|
$service = call_user_func($this->serviceNameDecorator, $service); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return $this->prefix($service); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @param string $key |
278
|
|
|
* @return string |
279
|
|
|
*/ |
280
|
|
|
private function prefix(string $key): string |
281
|
|
|
{ |
282
|
|
|
return $this->storageKeys->prefix() . $key; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param string $service |
287
|
|
|
* @return string |
288
|
|
|
*/ |
289
|
|
|
private function successKey(string $service): string |
290
|
|
|
{ |
291
|
|
|
return $this->key($service) . $this->storageKeys->success(); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @param string $service |
296
|
|
|
* @return string |
297
|
|
|
*/ |
298
|
|
|
private function failureKey(string $service): string |
299
|
|
|
{ |
300
|
|
|
return $this->key($service) . $this->storageKeys->failure(); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @param string $service |
305
|
|
|
* @return string |
306
|
|
|
*/ |
307
|
|
|
private function rejectionKey(string $service): string |
308
|
|
|
{ |
309
|
|
|
return $this->key($service) . $this->storageKeys->rejection(); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @param string $service |
314
|
|
|
* @return string |
315
|
|
|
*/ |
316
|
|
|
private function lastFailureKey(string $service): string |
317
|
|
|
{ |
318
|
|
|
return $this->supportSlidingTimeWindow() |
319
|
|
|
// If the adapter supports SlidingTimeWindow use failureKey() instead, |
320
|
|
|
// because Redis doesn't save lastFailureTime. |
321
|
|
|
// @see Ackintosh\Ganesha\Storage\Adapter\Redis#saveLastFailureTime() |
322
|
|
|
? $this->failureKey($service) |
323
|
|
|
: $this->prefix($service) . $this->storageKeys->lastFailureTime(); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @param string $service |
328
|
|
|
* @return string |
329
|
|
|
*/ |
330
|
|
|
private function statusKey(string $service): string |
331
|
|
|
{ |
332
|
|
|
return $this->prefix($service) . $this->storageKeys->status(); |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|