-
Notifications
You must be signed in to change notification settings - Fork 59
/
MultiSemaphore.php
145 lines (121 loc) · 4.69 KB
/
MultiSemaphore.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
141
142
143
144
145
<?php
namespace Consul\Helper;
use Consul\Helper\MultiSemaphore\Resource;
use Consul\Services\KV;
use Consul\Services\Session;
class MultiSemaphore
{
private Session $session;
private KV $kv;
private ?string $sessionId = null;
private array $resources;
private string $keyPrefix;
private int $ttl;
private string $metaDataKey = '.semaphore';
public function __construct(array $resources, int $ttl, Session $session, KV $kv, string $keyPrefix)
{
$this->resources = $resources;
$this->ttl = $ttl;
$this->session = $session;
$this->kv = $kv;
$this->keyPrefix = trim($keyPrefix, '/');
}
public function getResources(): array
{
return $this->resources;
}
public function acquire(): bool
{
if (null !== $this->sessionId) {
throw new \RuntimeException('Resources are acquired already');
}
// Start a session
$session = $this->session->create(['Name' => 'semaphore', 'LockDelay' => 0, 'TTL' => "{$this->ttl}s"])->json();
$this->sessionId = $session['ID'];
$result = false;
try {
$result = $this->acquireResources();
} finally {
if (!$result) {
$this->release();
}
}
return $result;
}
public function renew(): bool
{
return $this->session->renew($this->sessionId)->isSuccessful();
}
public function release(): void
{
if ($this->sessionId) {
foreach ($this->resources as $resource) {
$this->kv->delete($this->getResourceKey($resource, $this->sessionId));
}
$this->session->destroy($this->sessionId);
$this->sessionId = null;
}
}
private function acquireResources(): bool
{
$result = true;
foreach ($this->resources as $resource) {
if (false === $this->kv->put($this->getResourceKey($resource, $this->sessionId), '', ['acquire' => $this->sessionId])->json()) {
$result = false;
} else {
$semaphoreMetaDataValue = [
'limit' => $resource->getLimit(),
'sessions' => [],
];
// get actual metadata
$semaphoreDataItems = $this->kv->get($this->getResourceKeyPrefix($resource), ['recurse' => true])->json();
foreach ($semaphoreDataItems as $key => $item) {
if ($item['Key'] == $this->getResourceKey($resource, $this->metaDataKey)) {
$semaphoreMetaDataActual = $item;
$semaphoreMetaDataActual['Value'] = json_decode(base64_decode($semaphoreMetaDataActual['Value']), true);
unset($semaphoreDataItems[$key]);
break;
}
}
// build new metadata
if (isset($semaphoreMetaDataActual)) {
foreach ($semaphoreDataItems as $item) {
if (isset($item['Session'])) {
if (isset($semaphoreMetaDataActual['Value']['sessions'][$item['Session']])) {
$semaphoreMetaDataValue['sessions'][$item['Session']] = $semaphoreMetaDataActual['Value']['sessions'][$item['Session']];
}
} else {
$this->kv->delete($item['Key']);
}
}
}
$resource->setAcquired(
min($resource->getAcquire(), $semaphoreMetaDataValue['limit'] - array_sum($semaphoreMetaDataValue['sessions']))
);
// add new element to metadata and save it
if ($resource->getAcquired() > 0) {
$semaphoreMetaDataValue['sessions'][$this->sessionId] = $resource->getAcquired();
$result = $this->kv->put(
$this->getResourceKey($resource, $this->metaDataKey),
$semaphoreMetaDataValue,
['cas' => isset($semaphoreMetaDataActual) ? $semaphoreMetaDataActual['ModifyIndex'] : 0]
)->json();
} else {
$result = false;
}
}
if (!$result) {
break;
}
}
return $result;
}
private function getResourceKeyPrefix(Resource $resource): string
{
return $this->keyPrefix.'/'.$resource->getName();
}
private function getResourceKey(Resource $resource, string $name): string
{
return $this->getResourceKeyPrefix($resource).'/'.$name;
}
}