1 | <?php |
||
17 | final class MultipleStoreLocker implements Locker, LoggerAwareInterface |
||
18 | { |
||
19 | use LoggerAwareTrait; |
||
20 | |||
21 | /** @var LockStore[] */ |
||
22 | private $stores = []; |
||
23 | |||
24 | /** @var TokenGenerator */ |
||
25 | private $tokenGenerator; |
||
26 | |||
27 | /** @var Quorum */ |
||
28 | private $quorum; |
||
29 | |||
30 | /** @var Stopwatch */ |
||
31 | private $stopwatch; |
||
32 | |||
33 | /** |
||
34 | * MultipleStoreLocker constructor. |
||
35 | * |
||
36 | * @param LockStore[] $stores Array of persistence stores for the locks |
||
37 | * @param TokenGenerator $tokenGenerator The token generator |
||
38 | * @param Quorum $quorum The quorum implementation to use |
||
39 | * @param Stopwatch $stopwatch A way to measure time passed |
||
40 | */ |
||
41 | 36 | public function __construct( |
|
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | 12 | public function lock($resource, $ttl = null, $retryDelay = 0, $retryCount = 0) |
|
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | 9 | public function isLocked($resource) |
|
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | 12 | public function unlock(Lock $lock) |
|
110 | |||
111 | /** |
||
112 | * Try locking resource on all stores. |
||
113 | * |
||
114 | * Measure the time to do it and reject if not enough stores have successfully |
||
115 | * locked the resource or if time to lock on all stores have exceeded the ttl. |
||
116 | * |
||
117 | * @param Lock $lock The lock instance |
||
118 | * @param int $ttl Time to live in milliseconds |
||
119 | * |
||
120 | * @throws LockingException |
||
121 | * |
||
122 | * @return Lock |
||
123 | */ |
||
124 | 12 | private function lockAndCheckQuorumAndTtlOnAllStores(Lock $lock, $ttl) |
|
139 | |||
140 | /** |
||
141 | * Lock resource on all stores and count how many stores did it with success. |
||
142 | * |
||
143 | * @param Lock $lock The lock instance |
||
144 | * @param int $ttl Time to live in milliseconds |
||
145 | * |
||
146 | * @return int The number of stores on which the resource has been locked |
||
147 | */ |
||
148 | 12 | private function lockOnAllStores(Lock $lock, $ttl) |
|
160 | |||
161 | /** |
||
162 | * Unlock the resource on all stores. |
||
163 | * |
||
164 | * @param Lock $lock The lock to release |
||
165 | */ |
||
166 | 6 | private function resetLock($lock) |
|
172 | |||
173 | /** |
||
174 | * Init all stores passed to the constructor. |
||
175 | * |
||
176 | * If no store is given, it will return an InvalidArgumentException. |
||
177 | * |
||
178 | * @param LockStore[] $stores The lock stores |
||
179 | * |
||
180 | * @throws \InvalidArgumentException |
||
181 | */ |
||
182 | 36 | private function setStores(array $stores) |
|
190 | |||
191 | /** |
||
192 | * Set the quorum based on the number of stores passed to the constructor. |
||
193 | * |
||
194 | * @param Quorum $quorum The quorum implementation to use |
||
195 | */ |
||
196 | 36 | private function setQuorum(Quorum $quorum) |
|
201 | |||
202 | /** |
||
203 | * Check if the number of stores where the resource has been locked meet the quorum. |
||
204 | * |
||
205 | * @param int $storesLocked The number of stores on which the resource has been locked |
||
206 | * |
||
207 | * @throws LockingException |
||
208 | */ |
||
209 | 12 | private function checkQuorum($storesLocked) |
|
215 | |||
216 | /** |
||
217 | * Make the script wait before retrying to lock. |
||
218 | * |
||
219 | * @param int $retryDelay The retry delay in milliseconds |
||
220 | */ |
||
221 | 6 | private function waitBeforeRetrying($retryDelay) |
|
225 | |||
226 | /** |
||
227 | * Checks if the elapsed time is inferior to the ttl. |
||
228 | * |
||
229 | * To the elapsed time is added a drift time to have a margin of error. |
||
230 | * If this adjusted time is greater than the ttl, it will throw a LockingException. |
||
231 | * |
||
232 | * @param int $elapsedTime The time elapsed in milliseconds |
||
233 | * @param int $ttl The time to live in milliseconds |
||
234 | * |
||
235 | * @throws LockingException |
||
236 | */ |
||
237 | 6 | private function checkTtl($elapsedTime, $ttl) |
|
245 | |||
246 | /** |
||
247 | * Get the drift time based on ttl in ms. |
||
248 | * |
||
249 | * Return the max drift time of all stores |
||
250 | * |
||
251 | * @param int $ttl The time to live in milliseconds |
||
252 | * |
||
253 | * @return float |
||
254 | */ |
||
255 | private function getDrift($ttl) |
||
261 | } |
||
262 |