Skip to content

Commit 995e11d

Browse files
committed
Zookeeper add
1 parent 8a6e870 commit 995e11d

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

src/Competitor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
use Foxtech\Mutexes\MemcachedMutex;
1515
use Foxtech\Mutexes\PdoMutex;
1616
use Foxtech\Mutexes\RedisMutex;
17+
use Foxtech\Mutexes\ZookeeperMutex;
1718
use InvalidArgumentException;
1819
use PDO;
1920
use Memcached;
2021
use Redis;
2122
use RedisArray;
2223
use RedisCluster;
2324
use Predis\Client;
25+
use Zookeeper;
2426

2527
/**
2628
* Class Competitor
@@ -42,6 +44,7 @@ class Competitor
4244
RedisArray::class => RedisMutex::class,
4345
RedisCluster::class => RedisMutex::class,
4446
Client::class => RedisMutex::class,
47+
Zookeeper::class => ZookeeperMutex::class,
4548
];
4649

4750
/**

src/Mutexes/ZookeeperMutex.php

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
use Foxtech\AbstractMutex;
1515
use Foxtech\MutexInterface;
1616
use Zookeeper;
17+
use ZookeeperException;
18+
use LockConflictedException;
19+
use LockAcquiringException;
20+
use Exception;
1721

1822
/**
1923
* Class PdoMutex
@@ -25,21 +29,96 @@
2529
*/
2630
class ZookeeperMutex extends AbstractMutex implements MutexInterface
2731
{
32+
private $token;
33+
2834
/**
2935
* {@inheritdoc}
3036
* @see MutexInterface::acquire()
3137
*/
32-
public function acquire(): void
38+
public function acquire(string $key = null): void
3339
{
40+
if ($this->exists($key)) {
41+
return;
42+
}
3443

44+
$this->createNewLock($key, $this->getUniqueToken());
3545
}
3646

3747
/**
3848
* {@inheritdoc}
3949
* @see MutexInterface::release()
4050
*/
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
42117
{
118+
if (!$this->token) {
119+
$this->token = base64_encode(random_bytes(32));
120+
}
43121

122+
return $this->token;
44123
}
45124
}

0 commit comments

Comments
 (0)