1 | <?php |
||
41 | class RedisLockStrategy implements LockingStrategyInterface |
||
42 | { |
||
43 | /** |
||
44 | * @var \Redis A key-value data store |
||
45 | */ |
||
46 | private $redis; |
||
47 | |||
48 | /** |
||
49 | * @var string The locking subject, i.e. a string to discriminate the lock |
||
50 | */ |
||
51 | private $subject; |
||
52 | |||
53 | /** |
||
54 | * @var string The key used for the lock itself |
||
55 | */ |
||
56 | private $name; |
||
57 | |||
58 | /** |
||
59 | * @var string The key used for the mutex |
||
60 | */ |
||
61 | private $mutex; |
||
62 | |||
63 | /** |
||
64 | * @var string The value used for the lock |
||
65 | */ |
||
66 | private $value; |
||
67 | |||
68 | /** |
||
69 | * @var boolean TRUE if lock is acquired by this locker |
||
70 | */ |
||
71 | private $isAcquired = false; |
||
72 | |||
73 | /** |
||
74 | * @var int Seconds the lock remains persistent |
||
75 | */ |
||
76 | private $ttl = 60; |
||
77 | |||
78 | /** |
||
79 | * @inheritdoc |
||
80 | */ |
||
81 | 9 | public function __construct($subject) |
|
82 | { |
||
83 | 9 | $config = null; |
|
84 | 9 | if (\array_key_exists('redis_lock', $GLOBALS['TYPO3_CONF_VARS']['SYS'])) { |
|
85 | 8 | $config = $GLOBALS['TYPO3_CONF_VARS']['SYS']['redis_lock']; |
|
86 | } |
||
87 | |||
88 | 9 | if (!\is_array($config)) { |
|
89 | 2 | throw new LockCreateException('no configuration for redis lock strategy found'); |
|
90 | } |
||
91 | |||
92 | 7 | if (!\array_key_exists('host', $config)) { |
|
93 | 1 | throw new LockCreateException('no host for redis lock strategy found'); |
|
94 | } |
||
95 | |||
96 | 6 | $port = 6379; |
|
97 | 6 | if (\array_key_exists('port', $config)) { |
|
98 | $port = (int) $config['port']; |
||
99 | } |
||
100 | |||
101 | 6 | if (!\array_key_exists('database', $config)) { |
|
102 | 1 | throw new LockCreateException('no database for redis lock strategy found'); |
|
103 | } |
||
104 | |||
105 | 5 | if (\array_key_exists('ttl', $config)) { |
|
106 | $this->ttl = (int) $config['ttl']; |
||
107 | } |
||
108 | |||
109 | 5 | $this->redis = new \Redis(); |
|
|
|||
110 | 5 | $this->redis->connect($config['host'], $port); |
|
111 | 5 | if (\array_key_exists('auth', $config)) { |
|
112 | $this->redis->auth($config['auth']); |
||
113 | } |
||
114 | 5 | $this->redis->select((int) $config['database']); |
|
115 | |||
116 | 5 | $this->subject = $subject; |
|
117 | 5 | $this->name = sprintf('lock:name:%s', $subject); |
|
118 | 5 | $this->mutex = sprintf('lock:mutex:%s', $subject); |
|
119 | 5 | $this->value = uniqid(); |
|
120 | 5 | } |
|
121 | |||
122 | /** |
||
123 | * @inheritdoc |
||
124 | */ |
||
125 | 9 | public static function getCapabilities() |
|
126 | { |
||
127 | 9 | return self::LOCK_CAPABILITY_EXCLUSIVE | self::LOCK_CAPABILITY_NOBLOCK; |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * @inheritdoc |
||
132 | */ |
||
133 | 9 | public static function getPriority() |
|
134 | { |
||
135 | 9 | return 100; |
|
136 | } |
||
137 | |||
138 | /** |
||
139 | * @inheritdoc |
||
140 | */ |
||
141 | 4 | public function acquire($mode = self::LOCK_CAPABILITY_EXCLUSIVE) |
|
142 | { |
||
143 | 4 | if ($this->isAcquired) { |
|
144 | return true; |
||
145 | } |
||
146 | |||
147 | 4 | if ($mode & self::LOCK_CAPABILITY_EXCLUSIVE) { |
|
148 | 4 | if ($mode & self::LOCK_CAPABILITY_NOBLOCK) { |
|
149 | |||
150 | // try to acquire the lock - non-blocking |
||
151 | 1 | if (!$this->isAcquired = $this->lock()) { |
|
152 | 1 | throw new LockAcquireWouldBlockException('could not acquire lock'); |
|
153 | } |
||
154 | } else { |
||
155 | |||
156 | // try to acquire the lock - blocking |
||
157 | // N.B. we do this in a loop because between |
||
158 | // wait() and lock() another process may acquire the lock |
||
159 | 4 | while (!$this->isAcquired = $this->lock()) { |
|
160 | |||
161 | // this blocks till the lock gets released or timeout is reached |
||
162 | if (!$this->wait()) { |
||
163 | throw new LockAcquireException('could not acquire lock'); |
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | } else { |
||
168 | throw new LockAcquireException('insufficient capabilities'); |
||
169 | } |
||
170 | |||
171 | 4 | return $this->isAcquired; |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * @inheritdoc |
||
176 | */ |
||
177 | 2 | public function isAcquired() |
|
181 | |||
182 | /** |
||
183 | * @inheritdoc |
||
184 | */ |
||
185 | 1 | public function destroy() |
|
189 | |||
190 | /** |
||
191 | * @inheritdoc |
||
192 | */ |
||
193 | 2 | public function release() |
|
207 | |||
208 | /** |
||
209 | * Try to lock |
||
210 | * N.B. this a is non-blocking operation |
||
211 | * |
||
212 | * @return boolean TRUE on success, FALSE otherwise |
||
213 | */ |
||
214 | 4 | private function lock() |
|
221 | |||
222 | /** |
||
223 | * Wait on the mutex for the lock being released |
||
224 | * N.B. this a is blocking operation |
||
225 | * |
||
226 | * @return string The popped value, FALSE on timeout |
||
227 | */ |
||
228 | private function wait() |
||
235 | |||
236 | /** |
||
237 | * Try to unlock and if succeeds, signal the mutex |
||
238 | * N.B. by using EVAL we enforce transactional behaviour |
||
239 | * |
||
240 | * @return boolean TRUE on success, FALSE otherwise |
||
241 | */ |
||
242 | 1 | private function unlockAndSignal() |
|
253 | |||
254 | } |
||
255 |
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.