|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\Lock; |
| 15 | + |
| 16 | +use Closure; |
| 17 | +use CodeIgniter\Exceptions\InvalidArgumentException; |
| 18 | + |
| 19 | +class Lock implements LockInterface |
| 20 | +{ |
| 21 | + public function __construct( |
| 22 | + private readonly LockStoreInterface $store, |
| 23 | + private readonly string $key, |
| 24 | + private readonly int $ttl, |
| 25 | + private readonly string $owner, |
| 26 | + ) { |
| 27 | + if ($ttl < 1) { |
| 28 | + throw new InvalidArgumentException('Lock TTL must be a positive integer.'); |
| 29 | + } |
| 30 | + |
| 31 | + if ($owner === '') { |
| 32 | + throw new InvalidArgumentException('Lock owner cannot be empty.'); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public function acquire(): bool |
| 37 | + { |
| 38 | + return $this->store->acquireLock($this->key, $this->owner, $this->ttl); |
| 39 | + } |
| 40 | + |
| 41 | + public function block(int $seconds): bool |
| 42 | + { |
| 43 | + if ($seconds < 1) { |
| 44 | + return $this->acquire(); |
| 45 | + } |
| 46 | + |
| 47 | + $expiresAt = microtime(true) + $seconds; |
| 48 | + |
| 49 | + do { |
| 50 | + if ($this->acquire()) { |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + usleep(100_000); |
| 55 | + } while (microtime(true) < $expiresAt); |
| 56 | + |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @param Closure(): mixed $callback |
| 62 | + */ |
| 63 | + public function run(Closure $callback, int $waitSeconds = 0): mixed |
| 64 | + { |
| 65 | + $acquired = $waitSeconds > 0 ? $this->block($waitSeconds) : $this->acquire(); |
| 66 | + |
| 67 | + if (! $acquired) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + try { |
| 72 | + return $callback(); |
| 73 | + } finally { |
| 74 | + $this->release(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public function release(): bool |
| 79 | + { |
| 80 | + return $this->store->releaseLock($this->key, $this->owner); |
| 81 | + } |
| 82 | + |
| 83 | + public function forceRelease(): bool |
| 84 | + { |
| 85 | + return $this->store->forceReleaseLock($this->key); |
| 86 | + } |
| 87 | + |
| 88 | + public function refresh(?int $ttl = null): bool |
| 89 | + { |
| 90 | + $ttl ??= $this->ttl; |
| 91 | + |
| 92 | + if ($ttl < 1) { |
| 93 | + throw new InvalidArgumentException('Lock TTL must be a positive integer.'); |
| 94 | + } |
| 95 | + |
| 96 | + return $this->store->refreshLock($this->key, $this->owner, $ttl); |
| 97 | + } |
| 98 | + |
| 99 | + public function isAcquired(): bool |
| 100 | + { |
| 101 | + return $this->store->getLockOwner($this->key) === $this->owner; |
| 102 | + } |
| 103 | + |
| 104 | + public function owner(): string |
| 105 | + { |
| 106 | + return $this->owner; |
| 107 | + } |
| 108 | +} |
0 commit comments