← All changes
|
vendor/google/auth/src/Cache/SysVCacheItemPool.php
+26
-129
3.15.1
→
3.9.1
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,12 +31,10 @@ | ||
| 33 | 31 | const VAR_KEY = 1; |
| 34 | 32 | |
| 35 | 33 | const DEFAULT_PROJ = 'A'; |
| 36 | 34 | |
| 37 | - const DEFAULT_SEM_PROJ = 'B'; | |
| 35 | + const DEFAULT_MEMSIZE = 10000; | |
| 38 | 36 | |
| 39 | - const DEFAULT_MEMSIZE = 100000; | |
| 40 | - | |
| 41 | 37 | const DEFAULT_PERM = 0600; |
| 42 | 38 | |
| 43 | 39 | /** |
| 44 | 40 | * @var int |
| @@ -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. |
| @@ -85,11 +69,8 @@ | ||
| 85 | 69 | * |
| 86 | 70 | * @type int $variableKey The variable key for getting the data from the shared memory. **Defaults to** 1. |
| 87 | 71 | * @type string $proj The project identifier for ftok. This needs to be a one character string. |
| 88 | 72 | * **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'. | |
| 92 | 73 | * @type int $memsize The memory size in bytes for shm_attach. **Defaults to** 10000. |
| 93 | 74 | * @type int $perm The permission for shm_attach. **Defaults to** 0600. |
| 94 | 75 | * } |
| 95 | 76 | */ |
| @@ -94,9 +75,9 @@ | ||
| 94 | 75 | * } |
| 95 | 76 | */ |
| 96 | 77 | public function __construct($options = []) |
| 97 | 78 | { |
| 98 | - if (!extension_loaded('sysvshm')) { | |
| 79 | + if (! extension_loaded('sysvshm')) { | |
| 99 | 80 | throw new \RuntimeException( |
| 100 | 81 | 'sysvshm extension is required to use this ItemPool' |
| 101 | 82 | ); |
| 102 | 83 | } |
| @@ -102,9 +83,8 @@ | ||
| 102 | 83 | } |
| 103 | 84 | $this->options = $options + [ |
| 104 | 85 | 'variableKey' => self::VAR_KEY, |
| 105 | 86 | 'proj' => self::DEFAULT_PROJ, |
| 106 | - 'semProj' => self::DEFAULT_SEM_PROJ, | |
| 107 | 87 | 'memsize' => self::DEFAULT_MEMSIZE, |
| 108 | 88 | 'perm' => self::DEFAULT_PERM |
| 109 | 89 | ]; |
| 110 | 90 | $this->items = []; |
| @@ -109,15 +89,8 @@ | ||
| 109 | 89 | ]; |
| 110 | 90 | $this->items = []; |
| 111 | 91 | $this->deferredItems = []; |
| 112 | 92 | $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 | 93 | } |
| 121 | 94 | |
| 122 | 95 | /** |
| 123 | 96 | * @param mixed $key |
| @@ -136,12 +109,13 @@ | ||
| 136 | 109 | public function getItems(array $keys = []): iterable |
| 137 | 110 | { |
| 138 | 111 | $this->loadItems(); |
| 139 | 112 | $items = []; |
| 113 | + $itemClass = \PHP_VERSION_ID >= 80000 ? TypedItem::class : Item::class; | |
| 140 | 114 | foreach ($keys as $key) { |
| 141 | 115 | $items[$key] = $this->hasItem($key) ? |
| 142 | 116 | clone $this->items[$key] : |
| 143 | - new TypedItem($key); | |
| 117 | + new $itemClass($key); | |
| 144 | 118 | } |
| 145 | 119 | return $items; |
| 146 | 120 | } |
| 147 | 121 | |
| @@ -158,19 +132,11 @@ | ||
| 158 | 132 | * {@inheritdoc} |
| 159 | 133 | */ |
| 160 | 134 | public function clear(): bool |
| 161 | 135 | { |
| 162 | - if (!$this->acquireLock()) { | |
| 163 | - return false; | |
| 164 | - } | |
| 165 | - | |
| 166 | 136 | $this->items = []; |
| 167 | 137 | $this->deferredItems = []; |
| 168 | - $ret = $this->saveCurrentItems(); | |
| 169 | - | |
| 170 | - $this->resetShm(); | |
| 171 | - $this->releaseLock(); | |
| 172 | - return $ret; | |
| 138 | + return $this->saveCurrentItems(); | |
| 173 | 139 | } |
| 174 | 140 | |
| 175 | 141 | /** |
| 176 | 142 | * {@inheritdoc} |
| @@ -184,12 +150,8 @@ | ||
| 184 | 150 | * {@inheritdoc} |
| 185 | 151 | */ |
| 186 | 152 | public function deleteItems(array $keys): bool |
| 187 | 153 | { |
| 188 | - if (!$this->acquireLock()) { | |
| 189 | - return false; | |
| 190 | - } | |
| 191 | - | |
| 192 | 154 | if (!$this->hasLoadedItems) { |
| 193 | 155 | $this->loadItems(); |
| 194 | 156 | } |
| 195 | 157 | |
| @@ -195,13 +157,9 @@ | ||
| 195 | 157 | |
| 196 | 158 | foreach ($keys as $key) { |
| 197 | 159 | unset($this->items[$key]); |
| 198 | 160 | } |
| 199 | - $ret = $this->saveCurrentItems(); | |
| 200 | - | |
| 201 | - $this->resetShm(); | |
| 202 | - $this->releaseLock(); | |
| 203 | - return $ret; | |
| 161 | + return $this->saveCurrentItems(); | |
| 204 | 162 | } |
| 205 | 163 | |
| 206 | 164 | /** |
| 207 | 165 | * {@inheritdoc} |
| @@ -207,20 +165,14 @@ | ||
| 207 | 165 | * {@inheritdoc} |
| 208 | 166 | */ |
| 209 | 167 | public function save(CacheItemInterface $item): bool |
| 210 | 168 | { |
| 211 | - if (!$this->acquireLock()) { | |
| 212 | - return false; | |
| 213 | - } | |
| 214 | - | |
| 215 | 169 | if (!$this->hasLoadedItems) { |
| 216 | 170 | $this->loadItems(); |
| 217 | 171 | } |
| 218 | 172 | |
| 219 | 173 | $this->items[$item->getKey()] = $item; |
| 220 | - $ret = $this->saveCurrentItems(); | |
| 221 | - $this->releaseLock(); | |
| 222 | - return $ret; | |
| 174 | + return $this->saveCurrentItems(); | |
| 223 | 175 | } |
| 224 | 176 | |
| 225 | 177 | /** |
| 226 | 178 | * {@inheritdoc} |
| @@ -235,20 +187,14 @@ | ||
| 235 | 187 | * {@inheritdoc} |
| 236 | 188 | */ |
| 237 | 189 | public function commit(): bool |
| 238 | 190 | { |
| 239 | - if (!$this->acquireLock()) { | |
| 240 | - return false; | |
| 241 | - } | |
| 242 | - | |
| 243 | 191 | foreach ($this->deferredItems as $item) { |
| 244 | 192 | if ($this->save($item) === false) { |
| 245 | - $this->releaseLock(); | |
| 246 | 193 | return false; |
| 247 | 194 | } |
| 248 | 195 | } |
| 249 | 196 | $this->deferredItems = []; |
| 250 | - $this->releaseLock(); | |
| 251 | 197 | return true; |
| 252 | 198 | } |
| 253 | 199 | |
| 254 | 200 | /** |
| @@ -257,23 +203,22 @@ | ||
| 257 | 203 | * @return bool true when success, false upon failure |
| 258 | 204 | */ |
| 259 | 205 | private function saveCurrentItems() |
| 260 | 206 | { |
| 261 | - if (!$this->acquireLock()) { | |
| 262 | - return false; | |
| 263 | - } | |
| 264 | - | |
| 265 | - if (false !== $shmid = $this->attachShm()) { | |
| 266 | - $success = shm_put_var( | |
| 207 | + $shmid = shm_attach( | |
| 208 | + $this->sysvKey, | |
| 209 | + $this->options['memsize'], | |
| 210 | + $this->options['perm'] | |
| 211 | + ); | |
| 212 | + if ($shmid !== false) { | |
| 213 | + $ret = shm_put_var( | |
| 267 | 214 | $shmid, |
| 268 | 215 | $this->options['variableKey'], |
| 269 | 216 | $this->items |
| 270 | 217 | ); |
| 271 | 218 | shm_detach($shmid); |
| 272 | - $this->releaseLock(); | |
| 273 | - return $success; | |
| 219 | + return $ret; | |
| 274 | 220 | } |
| 275 | - $this->releaseLock(); | |
| 276 | 221 | return false; |
| 277 | 222 | } |
| 278 | 223 | |
| 279 | 224 | /** |
| @@ -282,71 +227,23 @@ | ||
| 282 | 227 | * @return bool true when success, false upon failure |
| 283 | 228 | */ |
| 284 | 229 | private function loadItems() |
| 285 | 230 | { |
| 286 | - if (!$this->acquireLock()) { | |
| 287 | - return false; | |
| 288 | - } | |
| 289 | - | |
| 290 | - if (false !== $shmid = $this->attachShm()) { | |
| 231 | + $shmid = shm_attach( | |
| 232 | + $this->sysvKey, | |
| 233 | + $this->options['memsize'], | |
| 234 | + $this->options['perm'] | |
| 235 | + ); | |
| 236 | + if ($shmid !== false) { | |
| 291 | 237 | $data = @shm_get_var($shmid, $this->options['variableKey']); |
| 292 | - $this->items = $data ?: []; | |
| 238 | + if (!empty($data)) { | |
| 239 | + $this->items = $data; | |
| 240 | + } else { | |
| 241 | + $this->items = []; | |
| 242 | + } | |
| 293 | 243 | shm_detach($shmid); |
| 294 | 244 | $this->hasLoadedItems = true; |
| 295 | - $this->releaseLock(); | |
| 296 | 245 | return true; |
| 297 | 246 | } |
| 298 | - $this->releaseLock(); | |
| 299 | 247 | 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 | 248 | } |
| 352 | 249 | } |