PluginProbe
Authorizer / 3.3.2
Authorizer v3.3.2
3.16.0 3.15.3 3.15.2 3.15.1 3.15.0 3.14.3 3.14.4 3.14.2 3.14.1 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.9.0 2.9.1 2.9.10 2.9.11 2.9.12 2.9.13 2.9.2 2.9.3 All 127 releases
← All changes | vendor/google/auth/src/Cache/SysVCacheItemPool.php +26 -130 3.14.3 → 3.3.2 View file →
@@ -17,10 +17,8 @@
17 17 namespace Google\Auth\Cache;
18 18
19 19 use Psr\Cache\CacheItemInterface;
20 20 use Psr\Cache\CacheItemPoolInterface;
21 -use SysvSemaphore;
22 -use SysvSharedMemory;
23 21
24 22 /**
25 23 * SystemV shared memory based CacheItemPool implementation.
26 24 *
@@ -33,10 +31,8 @@
33 31 const VAR_KEY = 1;
34 32
35 33 const DEFAULT_PROJ = 'A';
36 34
37 - const DEFAULT_SEM_PROJ = 'B';
38 -
39 35 const DEFAULT_MEMSIZE = 10000;
40 36
41 37 const DEFAULT_PERM = 0600;
42 38
@@ -65,20 +61,8 @@
65 61 */
66 62 private $hasLoadedItems = false;
67 63
68 64 /**
69 - * @var SysvSemaphore|false
70 - */
71 - private SysvSemaphore|false $semId = false;
72 -
73 - /**
74 - * Maintain the process which is currently holding the semaphore to prevent deadlock.
75 - *
76 - * @var int|null
77 - */
78 - private ?int $lockOwnerPid = null;
79 -
80 - /**
81 65 * Create a SystemV shared memory based CacheItemPool.
82 66 *
83 67 * @param array<mixed> $options {
84 68 * [optional] Configuration options.
@@ -83,13 +67,9 @@
83 67 * @param array<mixed> $options {
84 68 * [optional] Configuration options.
85 69 *
86 70 * @type int $variableKey The variable key for getting the data from the shared memory. **Defaults to** 1.
87 - * @type string $proj The project identifier for ftok. This needs to be a one character string.
88 - * **Defaults to** 'A'.
89 - * @type string $semProj The project identifier for ftok to provide to `sem_get`. This needs to be a one
90 - * character string.
91 - * **Defaults to** 'B'.
71 + * @type string $proj The project identifier for ftok. This needs to be a one character string. **Defaults to** 'A'.
92 72 * @type int $memsize The memory size in bytes for shm_attach. **Defaults to** 10000.
93 73 * @type int $perm The permission for shm_attach. **Defaults to** 0600.
94 74 * }
95 75 */
@@ -94,9 +74,9 @@
94 74 * }
95 75 */
96 76 public function __construct($options = [])
97 77 {
98 - if (!extension_loaded('sysvshm')) {
78 + if (! extension_loaded('sysvshm')) {
99 79 throw new \RuntimeException(
100 80 'sysvshm extension is required to use this ItemPool'
101 81 );
102 82 }
@@ -102,9 +82,8 @@
102 82 }
103 83 $this->options = $options + [
104 84 'variableKey' => self::VAR_KEY,
105 85 'proj' => self::DEFAULT_PROJ,
106 - 'semProj' => self::DEFAULT_SEM_PROJ,
107 86 'memsize' => self::DEFAULT_MEMSIZE,
108 87 'perm' => self::DEFAULT_PERM
109 88 ];
110 89 $this->items = [];
@@ -109,15 +88,8 @@
109 88 ];
110 89 $this->items = [];
111 90 $this->deferredItems = [];
112 91 $this->sysvKey = ftok(__FILE__, $this->options['proj']);
113 -
114 - // gracefully handle when `sysvsem` isn't loaded
115 - // @TODO(v2): throw an exception when the extension isn't loaded
116 - if (extension_loaded('sysvsem')) {
117 - $semKey = ftok(__FILE__, $this->options['semProj']);
118 - $this->semId = sem_get($semKey, 1, $this->options['perm'], true);
119 - }
120 92 }
121 93
122 94 /**
123 95 * @param mixed $key
@@ -136,12 +108,13 @@
136 108 public function getItems(array $keys = []): iterable
137 109 {
138 110 $this->loadItems();
139 111 $items = [];
112 + $itemClass = \PHP_VERSION_ID >= 80000 ? TypedItem::class : Item::class;
140 113 foreach ($keys as $key) {
141 114 $items[$key] = $this->hasItem($key) ?
142 115 clone $this->items[$key] :
143 - new TypedItem($key);
116 + new $itemClass($key);
144 117 }
145 118 return $items;
146 119 }
147 120
@@ -158,19 +131,11 @@
158 131 * {@inheritdoc}
159 132 */
160 133 public function clear(): bool
161 134 {
162 - if (!$this->acquireLock()) {
163 - return false;
164 - }
165 -
166 135 $this->items = [];
167 136 $this->deferredItems = [];
168 - $ret = $this->saveCurrentItems();
169 -
170 - $this->resetShm();
171 - $this->releaseLock();
172 - return $ret;
137 + return $this->saveCurrentItems();
173 138 }
174 139
175 140 /**
176 141 * {@inheritdoc}
@@ -184,12 +149,8 @@
184 149 * {@inheritdoc}
185 150 */
186 151 public function deleteItems(array $keys): bool
187 152 {
188 - if (!$this->acquireLock()) {
189 - return false;
190 - }
191 -
192 153 if (!$this->hasLoadedItems) {
193 154 $this->loadItems();
194 155 }
195 156
@@ -195,13 +156,9 @@
195 156
196 157 foreach ($keys as $key) {
197 158 unset($this->items[$key]);
198 159 }
199 - $ret = $this->saveCurrentItems();
200 -
201 - $this->resetShm();
202 - $this->releaseLock();
203 - return $ret;
160 + return $this->saveCurrentItems();
204 161 }
205 162
206 163 /**
207 164 * {@inheritdoc}
@@ -207,20 +164,14 @@
207 164 * {@inheritdoc}
208 165 */
209 166 public function save(CacheItemInterface $item): bool
210 167 {
211 - if (!$this->acquireLock()) {
212 - return false;
213 - }
214 -
215 168 if (!$this->hasLoadedItems) {
216 169 $this->loadItems();
217 170 }
218 171
219 172 $this->items[$item->getKey()] = $item;
220 - $ret = $this->saveCurrentItems();
221 - $this->releaseLock();
222 - return $ret;
173 + return $this->saveCurrentItems();
223 174 }
224 175
225 176 /**
226 177 * {@inheritdoc}
@@ -235,20 +186,14 @@
235 186 * {@inheritdoc}
236 187 */
237 188 public function commit(): bool
238 189 {
239 - if (!$this->acquireLock()) {
240 - return false;
241 - }
242 -
243 190 foreach ($this->deferredItems as $item) {
244 191 if ($this->save($item) === false) {
245 - $this->releaseLock();
246 192 return false;
247 193 }
248 194 }
249 195 $this->deferredItems = [];
250 - $this->releaseLock();
251 196 return true;
252 197 }
253 198
254 199 /**
@@ -257,23 +202,22 @@
257 202 * @return bool true when success, false upon failure
258 203 */
259 204 private function saveCurrentItems()
260 205 {
261 - if (!$this->acquireLock()) {
262 - return false;
263 - }
264 -
265 - if (false !== $shmid = $this->attachShm()) {
266 - $success = shm_put_var(
206 + $shmid = shm_attach(
207 + $this->sysvKey,
208 + $this->options['memsize'],
209 + $this->options['perm']
210 + );
211 + if ($shmid !== false) {
212 + $ret = shm_put_var(
267 213 $shmid,
268 214 $this->options['variableKey'],
269 215 $this->items
270 216 );
271 217 shm_detach($shmid);
272 - $this->releaseLock();
273 - return $success;
218 + return $ret;
274 219 }
275 - $this->releaseLock();
276 220 return false;
277 221 }
278 222
279 223 /**
@@ -282,71 +226,23 @@
282 226 * @return bool true when success, false upon failure
283 227 */
284 228 private function loadItems()
285 229 {
286 - if (!$this->acquireLock()) {
287 - return false;
288 - }
289 -
290 - if (false !== $shmid = $this->attachShm()) {
230 + $shmid = shm_attach(
231 + $this->sysvKey,
232 + $this->options['memsize'],
233 + $this->options['perm']
234 + );
235 + if ($shmid !== false) {
291 236 $data = @shm_get_var($shmid, $this->options['variableKey']);
292 - $this->items = $data ?: [];
237 + if (!empty($data)) {
238 + $this->items = $data;
239 + } else {
240 + $this->items = [];
241 + }
293 242 shm_detach($shmid);
294 243 $this->hasLoadedItems = true;
295 - $this->releaseLock();
296 244 return true;
297 245 }
298 - $this->releaseLock();
299 246 return false;
300 - }
301 -
302 - private function acquireLock(): bool
303 - {
304 - if ($this->semId === false) {
305 - // if `sysvsem` isn't loaded, or if `sem_get` fails, return true
306 - // this ensures BC with previous versions of the auth library.
307 - // @TODO consider better handling when `sem_get` fails.
308 - return true;
309 - }
310 -
311 - $currentPid = getmypid();
312 - if ($this->lockOwnerPid === $currentPid) {
313 - // We already have the lock
314 - return true;
315 - }
316 -
317 - if (sem_acquire($this->semId)) {
318 - $this->lockOwnerPid = (int) $currentPid;
319 - return true;
320 - }
321 - return false;
322 - }
323 -
324 - private function releaseLock(): bool
325 - {
326 - if ($this->semId === false || $this->lockOwnerPid !== getmypid()) {
327 - return true;
328 - }
329 -
330 - $this->lockOwnerPid = null;
331 - return sem_release($this->semId);
332 - }
333 -
334 - private function resetShm(): void
335 - {
336 - // Remove the shared memory segment and semaphore when clearing the cache
337 - $shmid = @shm_attach($this->sysvKey);
338 - if ($shmid !== false) {
339 - @shm_remove($shmid);
340 - @shm_detach($shmid);
341 - }
342 - }
343 -
344 - private function attachShm(): SysvSharedMemory|false
345 - {
346 - return shm_attach(
347 - $this->sysvKey,
348 - $this->options['memsize'],
349 - $this->options['perm']
350 - );
351 247 }
352 248 }