|
14 | 14 | use Foxtech\AbstractMutex; |
15 | 15 | use Foxtech\MutexInterface; |
16 | 16 | use Zookeeper; |
| 17 | +use ZookeeperException; |
| 18 | +use LockConflictedException; |
| 19 | +use LockAcquiringException; |
| 20 | +use Exception; |
17 | 21 |
|
18 | 22 | /** |
19 | 23 | * Class PdoMutex |
|
25 | 29 | */ |
26 | 30 | class ZookeeperMutex extends AbstractMutex implements MutexInterface |
27 | 31 | { |
| 32 | + private $token; |
| 33 | + |
28 | 34 | /** |
29 | 35 | * {@inheritdoc} |
30 | 36 | * @see MutexInterface::acquire() |
31 | 37 | */ |
32 | | - public function acquire(): void |
| 38 | + public function acquire(string $key = null): void |
33 | 39 | { |
| 40 | + if ($this->exists($key)) { |
| 41 | + return; |
| 42 | + } |
34 | 43 |
|
| 44 | + $this->createNewLock($key, $this->getUniqueToken()); |
35 | 45 | } |
36 | 46 |
|
37 | 47 | /** |
38 | 48 | * {@inheritdoc} |
39 | 49 | * @see MutexInterface::release() |
40 | 50 | */ |
41 | | - public function release(): void |
| 51 | + public function release(string $key = null): void |
| 52 | + { |
| 53 | + if (!$this->exists($key)) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + try { |
| 58 | + $this->handler->delete($key); |
| 59 | + } catch (ZookeeperException $exception) { |
| 60 | + throw new LockReleasingException($exception); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Function checks whether mutex exists |
| 66 | + * |
| 67 | + * @param string $key Mutex key |
| 68 | + * @return bool |
| 69 | + * |
| 70 | + * @throws ZookeeperException |
| 71 | + * @throws Exception |
| 72 | + */ |
| 73 | + public function exists(string $key): bool |
| 74 | + { |
| 75 | + try { |
| 76 | + return $this->handler->get($key) === $this->getUniqueToken(); |
| 77 | + } catch (ZookeeperException $e) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Creates a zookeeper node. |
| 84 | + * |
| 85 | + * @param string $node The node which needs to be created |
| 86 | + * @param string $value The value to be assigned to a zookeeper node |
| 87 | + * |
| 88 | + * @throws LockConflictedException |
| 89 | + * @throws LockAcquiringException |
| 90 | + */ |
| 91 | + private function createNewLock(string $node, string $value): void |
| 92 | + { |
| 93 | + try { |
| 94 | + $this->handler->create( |
| 95 | + $node, |
| 96 | + $value, |
| 97 | + [['perms' => Zookeeper::PERM_ALL, 'scheme' => 'world', 'id' => 'anyone']], |
| 98 | + Zookeeper::EPHEMERAL |
| 99 | + ); |
| 100 | + } catch (ZookeeperException $ex) { |
| 101 | + if (Zookeeper::NODEEXISTS === $ex->getCode()) { |
| 102 | + throw new LockConflictedException($ex); |
| 103 | + } |
| 104 | + |
| 105 | + throw new LockAcquiringException($ex); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Get unique token for store new mutex |
| 111 | + * |
| 112 | + * @return string |
| 113 | + * |
| 114 | + * @throws Exception |
| 115 | + */ |
| 116 | + private function getUniqueToken(): string |
42 | 117 | { |
| 118 | + if (!$this->token) { |
| 119 | + $this->token = base64_encode(random_bytes(32)); |
| 120 | + } |
43 | 121 |
|
| 122 | + return $this->token; |
44 | 123 | } |
45 | 124 | } |
0 commit comments