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 | 33 | public function __construct( |
|
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | 6 | public function lock($resource, $ttl = null, $retryDelay = 0, $retryCount = 0) |
|
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | 9 | public function isResourceLocked($resource) |
|
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | 12 | public function unlock(Lock $lock) |
|
95 | |||
96 | /** |
||
97 | * @param Lock $lock |
||
98 | * @param int $ttl |
||
99 | * |
||
100 | * @throws LockingException |
||
101 | */ |
||
102 | 6 | private function lockAllInstances($lock, $ttl) |
|
103 | { |
||
104 | 6 | $timeMeasure = $this->stopwatch->start($lock->getToken()); |
|
105 | |||
106 | 6 | foreach ($this->instances as $instance) { |
|
107 | 6 | if (!$this->lockInstance($instance, $lock, $ttl)) { |
|
108 | 2 | throw new LockingException(); |
|
109 | } |
||
110 | 4 | } |
|
111 | |||
112 | 6 | $timeMeasure->stop(); |
|
113 | |||
114 | 6 | if (!$ttl) { |
|
115 | return; |
||
116 | } |
||
117 | |||
118 | 6 | if (($ttl - ($timeMeasure->getDuration() + self::getDrift($ttl))) <= 0) { |
|
119 | 3 | throw new LockingException(); |
|
120 | } |
||
121 | |||
122 | 3 | $lock->setValidityTimeEnd($timeMeasure->getOrigin() + $ttl); |
|
123 | 3 | } |
|
124 | |||
125 | /** |
||
126 | * @param Lock $lock |
||
127 | */ |
||
128 | 3 | 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 | 6 | 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 | 15 | private function unlockInstance(\Redis $instance, Lock $lock) |
|
186 | |||
187 | /** |
||
188 | * @param \Redis[] $instances |
||
189 | * |
||
190 | * @throws \Exception |
||
191 | */ |
||
192 | 33 | private function setInstances(array $instances) |
|
206 | |||
207 | /** |
||
208 | * @param $retryDelay |
||
209 | */ |
||
210 | 3 | private function waitBeforeRetrying($retryDelay) |
|
214 | |||
215 | /** |
||
216 | * Get the drift time based on ttl in ms. |
||
217 | * |
||
218 | * @param int $ttl |
||
219 | * |
||
220 | * @return float |
||
221 | */ |
||
222 | 6 | private static function getDrift($ttl) |
|
233 | } |
||
234 |