1 | <?php |
||
13 | final class SingleStoreLocker implements Locker |
||
14 | { |
||
15 | /** @var LockStore */ |
||
16 | private $store; |
||
17 | |||
18 | /** @var TokenGenerator */ |
||
19 | private $tokenGenerator; |
||
20 | |||
21 | /** @var Stopwatch */ |
||
22 | private $stopwatch; |
||
23 | |||
24 | /** |
||
25 | * SingleStoreLocker constructor. |
||
26 | * |
||
27 | * @param LockStore $store The persisting store for the locks |
||
28 | * @param TokenGenerator $tokenGenerator The token generator |
||
29 | * @param Stopwatch $stopwatch A way to measure time passed |
||
30 | */ |
||
31 | public function __construct( |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | */ |
||
44 | public function lock($resource, $ttl = null, $retryDelay = 0, $retryCount = 0) |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public function isLocked($resource) |
||
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | public function unlock(Lock $lock) |
||
85 | |||
86 | /** |
||
87 | * Try locking resource on store. |
||
88 | * |
||
89 | * Measure the time to do it and reject if time to lock on store have exceeded the ttl. |
||
90 | * |
||
91 | * @param Lock $lock The lock instance |
||
92 | * @param int $ttl Time to live in milliseconds |
||
93 | * |
||
94 | * @throws LockingException |
||
95 | * |
||
96 | * @return Lock |
||
97 | */ |
||
98 | private function lockAndCheckQuorumAndTtl(Lock $lock, $ttl) |
||
111 | |||
112 | /** |
||
113 | * Make the script wait before retrying to lock. |
||
114 | * |
||
115 | * @param int $retryDelay The retry delay in milliseconds |
||
116 | */ |
||
117 | private function waitBeforeRetrying($retryDelay) |
||
121 | |||
122 | /** |
||
123 | * Checks if the elapsed time is inferior to the ttl. |
||
124 | * |
||
125 | * To the elapsed time is added a drift time to have a margin of error. |
||
126 | * If this adjusted time is greater than the ttl, it will throw a LockingException. |
||
127 | * |
||
128 | * @param int $elapsedTime The time elapsed in milliseconds |
||
129 | * @param int $ttl The time to live in milliseconds |
||
130 | * |
||
131 | * @throws LockingException |
||
132 | */ |
||
133 | private function checkTtl($elapsedTime, $ttl) |
||
141 | } |
||
142 |