1 | <?php |
||
12 | final class RedLock implements Locker |
||
13 | { |
||
14 | /** @var float */ |
||
15 | const CLOCK_DRIFT_FACTOR = 0.01; |
||
16 | |||
17 | /** @var \Redis[] */ |
||
18 | private $instances = []; |
||
19 | |||
20 | /** @var TokenGenerator */ |
||
21 | private $tokenGenerator; |
||
22 | |||
23 | /** @var Stopwatch */ |
||
24 | private $stopwatch; |
||
25 | |||
26 | /** @var int */ |
||
27 | private $quorum; |
||
28 | |||
29 | /** |
||
30 | * RedLock constructor. |
||
31 | * |
||
32 | * @param \Redis[] $instances Array of pre-connected \Redis objects |
||
33 | * @param TokenGenerator $tokenGenerator The token generator |
||
34 | * @param Stopwatch $stopwatch A way to measure time passed |
||
35 | */ |
||
36 | 39 | public function __construct( |
|
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | 12 | public function lock($resource, $ttl = null, $retryDelay = 0, $retryCount = 0) |
|
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | 9 | public function isLocked($resource) |
|
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | 12 | public function unlock(Lock $lock) |
|
101 | |||
102 | /** |
||
103 | * Try locking all Redis instances. |
||
104 | * |
||
105 | * Measure the time to do it and reject if not enough Redis instance have successfully |
||
106 | * locked the resource or if time to lock all instances have exceeded the ttl. |
||
107 | * |
||
108 | * @param Lock $lock The lock instance |
||
109 | * @param int $ttl Time to live in milliseconds |
||
110 | * |
||
111 | * @throws LockingException |
||
112 | * |
||
113 | * @return Lock |
||
114 | */ |
||
115 | 12 | private function monitoredLockingOfAllInstances(Lock $lock, $ttl) |
|
130 | |||
131 | /** |
||
132 | * Lock resource in Redis instances and count how many instance did it with success. |
||
133 | * |
||
134 | * @param Lock $lock The lock instance |
||
135 | * @param int $ttl Time to live in milliseconds |
||
136 | * |
||
137 | * @return int The number of instances locked |
||
138 | */ |
||
139 | 12 | private function lockInstances(Lock $lock, $ttl) |
|
151 | |||
152 | /** |
||
153 | * Lock the resource on a given Redis instance. |
||
154 | * |
||
155 | * @param \Redis $instance Server instance to be locked |
||
156 | * @param Lock $lock The lock instance |
||
157 | * @param int $ttl Time to live in milliseconds |
||
158 | * |
||
159 | * @return bool |
||
160 | */ |
||
161 | 12 | private function lockInstance(\Redis $instance, Lock $lock, $ttl) |
|
171 | |||
172 | /** |
||
173 | * Unlock the resource on all Redis instances. |
||
174 | * |
||
175 | * @param Lock $lock The lock to release |
||
176 | */ |
||
177 | 6 | private function resetLock($lock) |
|
183 | |||
184 | /** |
||
185 | * Checks if the resource exists on a given Redis instance. |
||
186 | * |
||
187 | * @param \Redis $instance The Redis instance |
||
188 | * @param string $resource The name of the resource |
||
189 | * |
||
190 | * @return bool |
||
191 | */ |
||
192 | 18 | private function isInstanceResourceLocked(\Redis $instance, $resource) |
|
196 | |||
197 | /** |
||
198 | * Unlock the resource on a given Redis instance. |
||
199 | * |
||
200 | * If the lock is successfully released, it will return true. |
||
201 | * If the token provided by the lock doesn't correspond to the token stored, it will return false. |
||
202 | * If the lock is not found, it will return false. |
||
203 | * |
||
204 | * @param \Redis $instance Server instance to be unlocked |
||
205 | * @param Lock $lock The lock to unlock |
||
206 | * |
||
207 | * @return bool |
||
208 | */ |
||
209 | 18 | private function unlockInstance(\Redis $instance, Lock $lock) |
|
225 | |||
226 | /** |
||
227 | * Init all Redis instances passed to the constructor. |
||
228 | * |
||
229 | * If no Redis instance is given, it will return a InvalidArgumentException. |
||
230 | * If one or more Redis instance is not connected, it will return a InvalidArgumentException. |
||
231 | * |
||
232 | * @param \Redis[] $instances The connected Redis instances |
||
233 | * |
||
234 | * @throws \InvalidArgumentException |
||
235 | */ |
||
236 | 39 | private function setInstances(array $instances) |
|
250 | |||
251 | /** |
||
252 | * Set the quorum based on the number of instances passed to the constructor. |
||
253 | */ |
||
254 | 39 | private function setQuorum() |
|
259 | |||
260 | /** |
||
261 | * Check if the number of instances that have been locked reach the quorum. |
||
262 | * |
||
263 | * @param int $instancesLocked The number of instances that have been locked |
||
264 | * |
||
265 | * @throws LockingException |
||
266 | */ |
||
267 | 12 | private function checkQuorum($instancesLocked) |
|
273 | |||
274 | /** |
||
275 | * Make the script wait before retrying to lock. |
||
276 | * |
||
277 | * @param int $retryDelay The retry delay in milliseconds |
||
278 | */ |
||
279 | 6 | private function waitBeforeRetrying($retryDelay) |
|
283 | |||
284 | /** |
||
285 | * Checks if the elapsed time is inferior to the ttl. |
||
286 | * |
||
287 | * To the elapsed time is added a drift time to have a margin of error. |
||
288 | * If this adjusted time is greater than the ttl, it will throw a LockingException. |
||
289 | * |
||
290 | * @param int $elapsedTime The time elapsed in milliseconds |
||
291 | * @param int $ttl The time to live in milliseconds |
||
292 | * |
||
293 | * @throws LockingException |
||
294 | */ |
||
295 | 6 | private static function checkTtl($elapsedTime, $ttl) |
|
303 | |||
304 | /** |
||
305 | * Get the drift time based on ttl in ms. |
||
306 | * |
||
307 | * @param int $ttl The time to live in milliseconds |
||
308 | * |
||
309 | * @return float |
||
310 | */ |
||
311 | 6 | private static function getDrift($ttl) |
|
322 | } |
||
323 |