-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSemaphore.php
140 lines (114 loc) · 4.56 KB
/
Semaphore.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Semaphore;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\Semaphore\Exception\InvalidArgumentException;
use Symfony\Component\Semaphore\Exception\RuntimeException;
use Symfony\Component\Semaphore\Exception\SemaphoreAcquiringException;
use Symfony\Component\Semaphore\Exception\SemaphoreExpiredException;
use Symfony\Component\Semaphore\Exception\SemaphoreReleasingException;
/**
* Semaphore is the default implementation of the SemaphoreInterface.
*
* @author Grégoire Pineau <lyrixx@lyrixx.info>
* @author Jérémy Derussé <jeremy@derusse.com>
*/
final class Semaphore implements SemaphoreInterface, LoggerAwareInterface
{
use LoggerAwareTrait;
private bool $dirty = false;
public function __construct(
private Key $key,
private PersistingStoreInterface $store,
private float $ttlInSecond = 300.0,
private bool $autoRelease = true,
) {
}
public function __sleep(): array
{
throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
}
public function __wakeup(): void
{
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
}
/**
* Automatically releases the underlying semaphore when the object is destructed.
*/
public function __destruct()
{
if (!$this->autoRelease || !$this->dirty || !$this->isAcquired()) {
return;
}
$this->release();
}
public function acquire(): bool
{
try {
$this->key->resetLifetime();
$this->store->save($this->key, $this->ttlInSecond);
$this->key->reduceLifetime($this->ttlInSecond);
$this->dirty = true;
$this->logger?->debug('Successfully acquired the "{resource}" semaphore.', ['resource' => $this->key]);
return true;
} catch (SemaphoreAcquiringException) {
$this->logger?->notice('Failed to acquire the "{resource}" semaphore. Someone else already acquired the semaphore.', ['resource' => $this->key]);
return false;
} catch (\Exception $e) {
$this->logger?->notice('Failed to acquire the "{resource}" semaphore.', ['resource' => $this->key, 'exception' => $e]);
throw new RuntimeException(\sprintf('Failed to acquire the "%s" semaphore.', $this->key), 0, $e);
}
}
public function refresh(?float $ttlInSecond = null): void
{
if (!$ttlInSecond ??= $this->ttlInSecond) {
throw new InvalidArgumentException('You have to define an expiration duration.');
}
try {
$this->key->resetLifetime();
$this->store->putOffExpiration($this->key, $ttlInSecond);
$this->key->reduceLifetime($ttlInSecond);
$this->dirty = true;
$this->logger?->debug('Expiration defined for "{resource}" semaphore for "{ttlInSecond}" seconds.', ['resource' => $this->key, 'ttlInSecond' => $ttlInSecond]);
} catch (SemaphoreExpiredException $e) {
$this->dirty = false;
$this->logger?->notice('Failed to define an expiration for the "{resource}" semaphore, the semaphore has expired.', ['resource' => $this->key]);
throw $e;
} catch (\Exception $e) {
$this->logger?->notice('Failed to define an expiration for the "{resource}" semaphore.', ['resource' => $this->key, 'exception' => $e]);
throw new RuntimeException(\sprintf('Failed to define an expiration for the "%s" semaphore.', $this->key), 0, $e);
}
}
public function isAcquired(): bool
{
return $this->dirty = $this->store->exists($this->key);
}
public function release(): void
{
try {
$this->store->delete($this->key);
$this->dirty = false;
} catch (SemaphoreReleasingException $e) {
throw $e;
} catch (\Exception $e) {
$this->logger?->notice('Failed to release the "{resource}" semaphore.', ['resource' => $this->key]);
throw new RuntimeException(\sprintf('Failed to release the "%s" semaphore.', $this->key), 0, $e);
}
}
public function isExpired(): bool
{
return $this->key->isExpired();
}
public function getRemainingLifetime(): ?float
{
return $this->key->getRemainingLifetime();
}
}