|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Consul\Helper; |
| 4 | + |
| 5 | +use Consul\Helper\MultiSemaphore\Resource; |
| 6 | +use Consul\Services\KV; |
| 7 | +use Consul\Services\Session; |
| 8 | +use RuntimeException; |
| 9 | + |
| 10 | +class MultiSemaphore |
| 11 | +{ |
| 12 | + private Session $session; |
| 13 | + private KV $kv; |
| 14 | + private ?string $sessionId = null; |
| 15 | + private array $resources; |
| 16 | + private string $keyPrefix; |
| 17 | + private int $ttl; |
| 18 | + private string $metaDataKey = '.semaphore'; |
| 19 | + |
| 20 | + public function __construct(array $resources, int $ttl, Session $session, KV $kv, string $keyPrefix) |
| 21 | + { |
| 22 | + $this->resources = $resources; |
| 23 | + $this->ttl = $ttl; |
| 24 | + $this->session = $session; |
| 25 | + $this->kv = $kv; |
| 26 | + $this->keyPrefix = trim($keyPrefix, '/'); |
| 27 | + } |
| 28 | + |
| 29 | + public function getResources(): array |
| 30 | + { |
| 31 | + return $this->resources; |
| 32 | + } |
| 33 | + |
| 34 | + public function acquire(): bool |
| 35 | + { |
| 36 | + if (null !== $this->sessionId) { |
| 37 | + throw new RuntimeException('Resources are acquired already'); |
| 38 | + } |
| 39 | + |
| 40 | + // Start a session |
| 41 | + $session = $this->session->create(['Name' => 'semaphore', 'LockDelay' => 0, 'TTL' => "{$this->ttl}s"])->json(); |
| 42 | + $this->sessionId = $session['ID']; |
| 43 | + |
| 44 | + $result = false; |
| 45 | + |
| 46 | + try { |
| 47 | + $result = $this->acquireResources(); |
| 48 | + } finally { |
| 49 | + if (!$result) { |
| 50 | + $this->release(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return $result; |
| 55 | + } |
| 56 | + |
| 57 | + public function renew(): bool |
| 58 | + { |
| 59 | + return $this->session->renew($this->sessionId)->isSuccessful(); |
| 60 | + } |
| 61 | + |
| 62 | + public function release(): void |
| 63 | + { |
| 64 | + if ($this->sessionId) { |
| 65 | + foreach ($this->resources as $resource) { |
| 66 | + $this->kv->delete($this->getResourceKey($resource, $this->sessionId)); |
| 67 | + } |
| 68 | + |
| 69 | + $this->session->destroy($this->sessionId); |
| 70 | + $this->sessionId = null; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private function acquireResources(): bool |
| 75 | + { |
| 76 | + $result = true; |
| 77 | + |
| 78 | + foreach ($this->resources as $resource) { |
| 79 | + if (false === $this->kv->put($this->getResourceKey($resource, $this->sessionId), '', ['acquire' => $this->sessionId])->json()) { |
| 80 | + $result = false; |
| 81 | + } else { |
| 82 | + $semaphoreMetaDataValue = [ |
| 83 | + 'limit' => $resource->getLimit(), |
| 84 | + 'sessions' => [], |
| 85 | + ]; |
| 86 | + |
| 87 | + // get actual metadata |
| 88 | + $semaphoreDataItems = $this->kv->get($this->getResourceKeyPrefix($resource), ['recurse' => true])->json(); |
| 89 | + foreach ($semaphoreDataItems as $key => $item) { |
| 90 | + if ($item['Key'] == $this->getResourceKey($resource, $this->metaDataKey)) { |
| 91 | + $semaphoreMetaDataActual = $item; |
| 92 | + $semaphoreMetaDataActual['Value'] = json_decode(base64_decode($semaphoreMetaDataActual['Value']), true); |
| 93 | + unset($semaphoreDataItems[$key]); |
| 94 | + |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // build new metadata |
| 100 | + if (isset($semaphoreMetaDataActual)) { |
| 101 | + foreach ($semaphoreDataItems as $item) { |
| 102 | + if (isset($item['Session'])) { |
| 103 | + if (isset($semaphoreMetaDataActual['Value']['sessions'][$item['Session']])) { |
| 104 | + $semaphoreMetaDataValue['sessions'][$item['Session']] = $semaphoreMetaDataActual['Value']['sessions'][$item['Session']]; |
| 105 | + } |
| 106 | + } else { |
| 107 | + $this->kv->delete($item['Key']); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + $resource->setAcquired( |
| 113 | + min($resource->getAcquire(), ($semaphoreMetaDataValue['limit'] - array_sum($semaphoreMetaDataValue['sessions']))) |
| 114 | + ); |
| 115 | + |
| 116 | + // add new element to metadata and save it |
| 117 | + if ($resource->getAcquired() > 0) { |
| 118 | + $semaphoreMetaDataValue['sessions'][$this->sessionId] = $resource->getAcquired(); |
| 119 | + $result = $this->kv->put( |
| 120 | + $this->getResourceKey($resource, $this->metaDataKey), |
| 121 | + $semaphoreMetaDataValue, |
| 122 | + ['cas' => isset($semaphoreMetaDataActual) ? $semaphoreMetaDataActual['ModifyIndex'] : 0] |
| 123 | + )->json(); |
| 124 | + } else { |
| 125 | + $result = false; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if (!$result) { |
| 130 | + break; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return $result; |
| 135 | + } |
| 136 | + |
| 137 | + private function getResourceKeyPrefix(Resource $resource): string |
| 138 | + { |
| 139 | + return $this->keyPrefix.'/'.$resource->getName(); |
| 140 | + } |
| 141 | + |
| 142 | + private function getResourceKey(Resource $resource, string $name): string |
| 143 | + { |
| 144 | + return $this->getResourceKeyPrefix($resource).'/'.$name; |
| 145 | + } |
| 146 | +} |
0 commit comments