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 | /** |
||
27 | * @param \Redis[] $instances Array of pre-connected \Redis objects |
||
28 | * @param TokenGenerator $tokenGenerator The token generator |
||
29 | * @param Stopwatch $stopwatch A way to measure time passed |
||
30 | */ |
||
31 | 39 | public function __construct( |
|
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | 12 | public function lock($resource, $ttl = null, $retryDelay = 0, $retryCount = 0) |
|
47 | { |
||
48 | 12 | $lock = new Lock($resource, $this->tokenGenerator->generateToken()); |
|
49 | |||
50 | 12 | $tried = 0; |
|
51 | 12 | while (true) { |
|
52 | try { |
||
53 | 12 | $this->lockAllInstances($lock, $ttl); |
|
54 | |||
55 | 6 | return $lock; |
|
56 | 6 | } catch (LockingException $e) { |
|
57 | 6 | $this->resetLock($lock); |
|
58 | } |
||
59 | |||
60 | 6 | if ($tried++ == $retryCount) { |
|
61 | 6 | break; |
|
62 | } |
||
63 | |||
64 | 6 | $this->waitBeforeRetrying($retryDelay); |
|
65 | 4 | } |
|
66 | |||
67 | 6 | throw new LockingException(); |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | 9 | public function isResourceLocked($resource) |
|
74 | { |
||
75 | 9 | foreach ($this->instances as $instance) { |
|
76 | 9 | if ($this->isInstanceResourceLocked($instance, $resource)) { |
|
77 | 7 | return true; |
|
78 | } |
||
79 | 4 | } |
|
80 | |||
81 | 3 | return false; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 12 | public function unlock(Lock $lock) |
|
88 | { |
||
89 | 12 | foreach ($this->instances as $instance) { |
|
90 | 12 | if (!$this->unlockInstance($instance, $lock)) { |
|
91 | 9 | if ($this->isInstanceResourceLocked($instance, $lock->getResource())) { |
|
92 | 8 | throw new UnlockingException(); // Only throw an exception if the lock is still present |
|
93 | } |
||
94 | 2 | } |
|
95 | 6 | } |
|
96 | 6 | } |
|
97 | |||
98 | /** |
||
99 | * @param Lock $lock |
||
100 | * @param int $ttl |
||
101 | * |
||
102 | * @throws LockingException |
||
103 | */ |
||
104 | 12 | private function lockAllInstances($lock, $ttl) |
|
105 | { |
||
106 | 12 | $timeMeasure = $this->stopwatch->start($lock->getToken()); |
|
107 | |||
108 | 12 | foreach ($this->instances as $instance) { |
|
109 | 12 | if (!$this->lockInstance($instance, $lock, $ttl)) { |
|
110 | 6 | throw new LockingException(); |
|
111 | } |
||
112 | 8 | } |
|
113 | |||
114 | 9 | $timeMeasure->stop(); |
|
115 | |||
116 | 9 | if (!$ttl) { |
|
117 | 3 | return; |
|
118 | } |
||
119 | |||
120 | 6 | self::checkTtl($timeMeasure->getDuration(), $ttl); |
|
121 | |||
122 | 3 | $lock->setValidityTimeEnd($timeMeasure->getOrigin() + $ttl); |
|
123 | 3 | } |
|
124 | |||
125 | /** |
||
126 | * @param Lock $lock |
||
127 | */ |
||
128 | 6 | private function resetLock($lock) |
|
134 | |||
135 | /** |
||
136 | * @param \Redis $instance Server instance to be locked |
||
137 | * @param Lock $lock The lock instance |
||
138 | * @param int $ttl Time to live in milliseconds |
||
139 | * |
||
140 | * @return bool |
||
141 | */ |
||
142 | 12 | private function lockInstance(\Redis $instance, Lock $lock, $ttl) |
|
152 | |||
153 | /** |
||
154 | * @param \Redis $instance |
||
155 | * @param string $resource |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | 18 | private function isInstanceResourceLocked(\Redis $instance, $resource) |
|
163 | |||
164 | /** |
||
165 | * @param \Redis $instance Server instance to be unlocked |
||
166 | * @param Lock $lock The lock to unlock |
||
167 | * |
||
168 | * @return bool |
||
169 | */ |
||
170 | 18 | private function unlockInstance(\Redis $instance, Lock $lock) |
|
186 | |||
187 | /** |
||
188 | * @param \Redis[] $instances |
||
189 | * |
||
190 | * @throws \Exception |
||
191 | */ |
||
192 | 39 | private function setInstances(array $instances) |
|
206 | |||
207 | /** |
||
208 | * @param $retryDelay |
||
209 | */ |
||
210 | 6 | private function waitBeforeRetrying($retryDelay) |
|
214 | |||
215 | /** |
||
216 | * @param int $elapsedTime |
||
217 | * @param int $ttl |
||
218 | * |
||
219 | * @throws LockingException |
||
220 | */ |
||
221 | 6 | private static function checkTtl($elapsedTime, $ttl) |
|
229 | |||
230 | /** |
||
231 | * Get the drift time based on ttl in ms. |
||
232 | * |
||
233 | * @param int $ttl |
||
234 | * |
||
235 | * @return float |
||
236 | */ |
||
237 | 6 | private static function getDrift($ttl) |
|
248 | } |
||
249 |