1 | <?php |
||
16 | final class SingleStoreLocker implements Locker, LoggerAwareInterface |
||
17 | { |
||
18 | use LoggerAwareTrait; |
||
19 | |||
20 | /** @var LockStore */ |
||
21 | private $store; |
||
22 | |||
23 | /** @var TokenGenerator */ |
||
24 | private $tokenGenerator; |
||
25 | |||
26 | /** @var Stopwatch */ |
||
27 | private $stopwatch; |
||
28 | |||
29 | /** |
||
30 | * SingleStoreLocker constructor. |
||
31 | * |
||
32 | * @param LockStore $store The persisting store for the locks |
||
33 | * @param TokenGenerator $tokenGenerator The token generator |
||
34 | * @param Stopwatch $stopwatch A way to measure time passed |
||
35 | */ |
||
36 | 21 | public function __construct( |
|
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | 9 | public function lock($resource, $ttl = null, $retryDelay = 0, $retryCount = 0) |
|
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | 3 | public function isLocked($resource) |
|
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | 9 | public function unlock(Lock $lock) |
|
93 | |||
94 | /** |
||
95 | * Try locking resource on store. |
||
96 | * |
||
97 | * Measure the time to do it and reject if time to lock on store have exceeded the ttl. |
||
98 | * |
||
99 | * @param Lock $lock The lock instance |
||
100 | * @param int $ttl Time to live in milliseconds |
||
101 | * |
||
102 | * @throws LockingException |
||
103 | * |
||
104 | * @return Lock |
||
105 | */ |
||
106 | 9 | private function lockAndCheckQuorumAndTtl(Lock $lock, $ttl) |
|
119 | |||
120 | /** |
||
121 | * Make the script wait before retrying to lock. |
||
122 | * |
||
123 | * @param int $retryDelay The retry delay in milliseconds |
||
124 | */ |
||
125 | 3 | private function waitBeforeRetrying($retryDelay) |
|
129 | |||
130 | /** |
||
131 | * Checks if the elapsed time is inferior to the ttl. |
||
132 | * |
||
133 | * To the elapsed time is added a drift time to have a margin of error. |
||
134 | * If this adjusted time is greater than the ttl, it will throw a LockingException. |
||
135 | * |
||
136 | * @param int $elapsedTime The time elapsed in milliseconds |
||
137 | * @param int $ttl The time to live in milliseconds |
||
138 | * |
||
139 | * @throws LockingException |
||
140 | */ |
||
141 | 6 | private function checkTtl($elapsedTime, $ttl) |
|
149 | } |
||
150 |