1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WyriHaximus\React\Mutex; |
6
|
|
|
|
7
|
|
|
use React\Cache\ArrayCache; |
8
|
|
|
use React\Cache\CacheInterface; |
9
|
|
|
use React\Promise\PromiseInterface; |
10
|
|
|
use WyriHaximus\React\Mutex\Contracts\LockInterface; |
11
|
|
|
use WyriHaximus\React\Mutex\Contracts\MutexInterface; |
12
|
1 |
|
|
13
|
|
|
use function bin2hex; |
14
|
1 |
|
use function random_bytes; |
15
|
1 |
|
use function React\Promise\resolve; |
16
|
|
|
use function WyriHaximus\React\timedPromise; |
17
|
|
|
|
18
|
1 |
|
final class Memory implements MutexInterface |
19
|
1 |
|
{ |
20
|
|
|
private const NO_MORE_ATTEMPTS_LEFT = 0; |
21
|
1 |
|
private const RANDOM_BYTES_LENGTH = 13; |
22
|
|
|
private CacheInterface $locks; |
23
|
|
|
|
24
|
|
|
public function __construct() |
25
|
|
|
{ |
26
|
|
|
$this->locks = new ArrayCache(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function acquire(string $key, float $ttl): PromiseInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @phpstan-ignore-next-line |
33
|
|
|
* @psalm-suppress TooManyTemplateParams |
34
|
|
|
*/ |
35
|
|
|
return $this->locks->has($key)->then(function (bool $has) use ($key, $ttl): ?Lock { |
36
|
|
|
if ($has) { |
37
|
|
|
return null; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$rng = bin2hex(random_bytes(self::RANDOM_BYTES_LENGTH)); |
41
|
|
|
$lock = new Lock($key, $rng); |
42
|
|
|
$this->locks->set($key, $lock, $ttl); |
43
|
|
|
|
44
|
|
|
return $lock; |
45
|
|
|
}); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function spin(string $key, float $ttl, int $attempts, float $interval): PromiseInterface |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* @phpstan-ignore-next-line |
52
|
|
|
*/ |
53
|
|
|
return $this->acquire($key, $ttl)->then(function (?LockInterface $lock) use ($key, $ttl, $attempts, $interval): PromiseInterface { |
54
|
|
|
if ($lock instanceof LockInterface || $attempts === self::NO_MORE_ATTEMPTS_LEFT) { |
55
|
|
|
return resolve($lock); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return timedPromise($interval, [$key, $ttl, --$attempts, $interval])->then(function (array $args): PromiseInterface { |
59
|
|
|
return $this->spin(...$args); |
60
|
|
|
}); |
61
|
|
|
}); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function release(LockInterface $lock): PromiseInterface |
65
|
|
|
{ |
66
|
|
|
/** |
67
|
|
|
* @psalm-suppress TooManyTemplateParams |
68
|
|
|
*/ |
69
|
|
|
return $this->locks->has($lock->key())->then(function (bool $has) use ($lock) { |
70
|
|
|
return $has ? $this->locks->get($lock->key()) : $has; |
71
|
|
|
})->then( |
72
|
|
|
/** @phpstan-ignore-next-line */ |
73
|
|
|
function (?Lock $storedLock) use ($lock) { |
74
|
|
|
if ($storedLock instanceof Lock && $storedLock->rng() === $lock->rng()) { |
75
|
|
|
return $this->locks->delete($lock->key()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return ! ($storedLock instanceof Lock) || $storedLock->rng() === $lock->rng(); |
79
|
|
|
} |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|