1 | <?php |
||
14 | final class MultipleStoreLocker implements Locker |
||
15 | { |
||
16 | /** @var LockStore[] */ |
||
17 | private $stores = []; |
||
18 | |||
19 | /** @var TokenGenerator */ |
||
20 | private $tokenGenerator; |
||
21 | |||
22 | /** @var Quorum */ |
||
23 | private $quorum; |
||
24 | |||
25 | /** @var Stopwatch */ |
||
26 | private $stopwatch; |
||
27 | |||
28 | /** |
||
29 | * RedLock constructor. |
||
30 | * |
||
31 | * @param LockStore[] $stores Array of persistence system connections |
||
32 | * @param TokenGenerator $tokenGenerator The token generator |
||
33 | * @param Quorum $quorum The quorum implementation to use |
||
34 | * @param Stopwatch $stopwatch A way to measure time passed |
||
35 | */ |
||
36 | public function __construct( |
||
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | public function lock($resource, $ttl = null, $retryDelay = 0, $retryCount = 0) |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | public function isLocked($resource) |
||
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | public function unlock(Lock $lock) |
||
102 | |||
103 | /** |
||
104 | * Try locking resource on all stores. |
||
105 | * |
||
106 | * Measure the time to do it and reject if not enough stores have successfully |
||
107 | * locked the resource or if time to lock on all stores have exceeded the ttl. |
||
108 | * |
||
109 | * @param Lock $lock The lock instance |
||
110 | * @param int $ttl Time to live in milliseconds |
||
111 | * |
||
112 | * @throws LockingException |
||
113 | * |
||
114 | * @return Lock |
||
115 | */ |
||
116 | private function lockAndCheckQuorumAndTtlOnAllStores(Lock $lock, $ttl) |
||
131 | |||
132 | /** |
||
133 | * Lock resource on all stores and count how many stores did it with success. |
||
134 | * |
||
135 | * @param Lock $lock The lock instance |
||
136 | * @param int $ttl Time to live in milliseconds |
||
137 | * |
||
138 | * @return int The number of stores on which the resource has been locked |
||
139 | */ |
||
140 | private function lockOnAllStores(Lock $lock, $ttl) |
||
152 | |||
153 | /** |
||
154 | * Unlock the resource on all stores. |
||
155 | * |
||
156 | * @param Lock $lock The lock to release |
||
157 | */ |
||
158 | private function resetLock($lock) |
||
164 | |||
165 | /** |
||
166 | * Init all stores passed to the constructor. |
||
167 | * |
||
168 | * If no store is given, it will return an InvalidArgumentException. |
||
169 | * |
||
170 | * @param LockStore[] $stores The lock stores |
||
171 | * |
||
172 | * @throws \InvalidArgumentException |
||
173 | */ |
||
174 | private function setStores(array $stores) |
||
182 | |||
183 | /** |
||
184 | * Set the quorum based on the number of stores passed to the constructor. |
||
185 | * |
||
186 | * @param Quorum $quorum The quorum implementation to use |
||
187 | */ |
||
188 | private function setQuorum(Quorum $quorum) |
||
193 | |||
194 | /** |
||
195 | * Check if the number of stores where the resource has been locked meet the quorum. |
||
196 | * |
||
197 | * @param int $storesLocked The number of stores on which the resource has been locked |
||
198 | * |
||
199 | * @throws LockingException |
||
200 | */ |
||
201 | private function checkQuorum($storesLocked) |
||
207 | |||
208 | /** |
||
209 | * Make the script wait before retrying to lock. |
||
210 | * |
||
211 | * @param int $retryDelay The retry delay in milliseconds |
||
212 | */ |
||
213 | private function waitBeforeRetrying($retryDelay) |
||
217 | |||
218 | /** |
||
219 | * Checks if the elapsed time is inferior to the ttl. |
||
220 | * |
||
221 | * To the elapsed time is added a drift time to have a margin of error. |
||
222 | * If this adjusted time is greater than the ttl, it will throw a LockingException. |
||
223 | * |
||
224 | * @param int $elapsedTime The time elapsed in milliseconds |
||
225 | * @param int $ttl The time to live in milliseconds |
||
226 | * |
||
227 | * @throws LockingException |
||
228 | */ |
||
229 | private function checkTtl($elapsedTime, $ttl) |
||
237 | |||
238 | /** |
||
239 | * Get the drift time based on ttl in ms. |
||
240 | * |
||
241 | * Return the max drift time of all stores |
||
242 | * |
||
243 | * @param int $ttl The time to live in milliseconds |
||
244 | * |
||
245 | * @return float |
||
246 | */ |
||
247 | private function getDrift($ttl) |
||
253 | } |
||
254 |