1 | <?php |
||
14 | final class MultipleInstanceLocker implements Locker |
||
15 | { |
||
16 | /** @var Connection[] */ |
||
17 | private $instances = []; |
||
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 Connection\[] $instances 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 | 36 | public function __construct( |
|
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | 12 | public function lock($resource, $ttl = null, $retryDelay = 0, $retryCount = 0) |
|
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | 9 | public function isLocked($resource) |
|
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | 12 | public function unlock(Lock $lock) |
|
102 | |||
103 | /** |
||
104 | * Try locking all connected instances. |
||
105 | * |
||
106 | * Measure the time to do it and reject if not enough connected instance have successfully |
||
107 | * locked the resource or if time to lock all instances 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 | 12 | private function monitoredLockingOfAllInstances(Lock $lock, $ttl) |
|
131 | |||
132 | /** |
||
133 | * Lock resource in connected instances and count how many instance 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 instances locked |
||
139 | */ |
||
140 | 12 | private function lockInstances(Lock $lock, $ttl) |
|
152 | |||
153 | /** |
||
154 | * Unlock the resource on all Redis instances. |
||
155 | * |
||
156 | * @param Lock $lock The lock to release |
||
157 | */ |
||
158 | 6 | private function resetLock($lock) |
|
164 | |||
165 | /** |
||
166 | * Init all Redis instances passed to the constructor. |
||
167 | * |
||
168 | * If no Redis instance is given, it will return a InvalidArgumentException. |
||
169 | * If one or more Redis instance is not connected, it will return a InvalidArgumentException. |
||
170 | * |
||
171 | * @param \Redis[] $instances The connected Redis instances |
||
172 | * |
||
173 | * @throws \InvalidArgumentException |
||
174 | */ |
||
175 | 36 | private function setInstances(array $instances) |
|
183 | |||
184 | /** |
||
185 | * Set the quorum based on the number of instances passed to the constructor. |
||
186 | * |
||
187 | * @param Quorum $quorum The quorum implementation to use |
||
188 | */ |
||
189 | 36 | private function setQuorum(Quorum $quorum) |
|
194 | |||
195 | /** |
||
196 | * Check if the number of instances that have been locked reach the quorum. |
||
197 | * |
||
198 | * @param int $instancesLocked The number of instances that have been locked |
||
199 | * |
||
200 | * @throws LockingException |
||
201 | */ |
||
202 | 12 | private function checkQuorum($instancesLocked) |
|
208 | |||
209 | /** |
||
210 | * Make the script wait before retrying to lock. |
||
211 | * |
||
212 | * @param int $retryDelay The retry delay in milliseconds |
||
213 | */ |
||
214 | 6 | private function waitBeforeRetrying($retryDelay) |
|
218 | |||
219 | /** |
||
220 | * Checks if the elapsed time is inferior to the ttl. |
||
221 | * |
||
222 | * To the elapsed time is added a drift time to have a margin of error. |
||
223 | * If this adjusted time is greater than the ttl, it will throw a LockingException. |
||
224 | * |
||
225 | * @param int $elapsedTime The time elapsed in milliseconds |
||
226 | * @param int $ttl The time to live in milliseconds |
||
227 | * |
||
228 | * @throws LockingException |
||
229 | */ |
||
230 | 6 | private function checkTtl($elapsedTime, $ttl) |
|
238 | |||
239 | /** |
||
240 | * Get the drift time based on ttl in ms. |
||
241 | * |
||
242 | * @param int $ttl The time to live in milliseconds |
||
243 | * |
||
244 | * @return float |
||
245 | */ |
||
246 | 6 | private function getDrift($ttl) |
|
250 | } |
||
251 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..