Skip to content

Commit

Permalink
Zookeeper add
Browse files Browse the repository at this point in the history
  • Loading branch information
foxtech6 committed Oct 8, 2019
1 parent 8a6e870 commit 995e11d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Competitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
use Foxtech\Mutexes\MemcachedMutex;
use Foxtech\Mutexes\PdoMutex;
use Foxtech\Mutexes\RedisMutex;
use Foxtech\Mutexes\ZookeeperMutex;
use InvalidArgumentException;
use PDO;
use Memcached;
use Redis;
use RedisArray;
use RedisCluster;
use Predis\Client;
use Zookeeper;

/**
* Class Competitor
Expand All @@ -42,6 +44,7 @@ class Competitor
RedisArray::class => RedisMutex::class,
RedisCluster::class => RedisMutex::class,
Client::class => RedisMutex::class,
Zookeeper::class => ZookeeperMutex::class,
];

/**
Expand Down
83 changes: 81 additions & 2 deletions src/Mutexes/ZookeeperMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
use Foxtech\AbstractMutex;
use Foxtech\MutexInterface;
use Zookeeper;
use ZookeeperException;
use LockConflictedException;
use LockAcquiringException;
use Exception;

/**
* Class PdoMutex
Expand All @@ -25,21 +29,96 @@
*/
class ZookeeperMutex extends AbstractMutex implements MutexInterface
{
private $token;

/**
* {@inheritdoc}
* @see MutexInterface::acquire()
*/
public function acquire(): void
public function acquire(string $key = null): void
{
if ($this->exists($key)) {
return;
}

$this->createNewLock($key, $this->getUniqueToken());
}

/**
* {@inheritdoc}
* @see MutexInterface::release()
*/
public function release(): void
public function release(string $key = null): void
{
if (!$this->exists($key)) {
return;
}

try {
$this->handler->delete($key);
} catch (ZookeeperException $exception) {
throw new LockReleasingException($exception);
}
}

/**
* Function checks whether mutex exists
*
* @param string $key Mutex key
* @return bool
*
* @throws ZookeeperException
* @throws Exception
*/
public function exists(string $key): bool
{
try {
return $this->handler->get($key) === $this->getUniqueToken();
} catch (ZookeeperException $e) {
return false;
}
}

/**
* Creates a zookeeper node.
*
* @param string $node The node which needs to be created
* @param string $value The value to be assigned to a zookeeper node
*
* @throws LockConflictedException
* @throws LockAcquiringException
*/
private function createNewLock(string $node, string $value): void
{
try {
$this->handler->create(
$node,
$value,
[['perms' => Zookeeper::PERM_ALL, 'scheme' => 'world', 'id' => 'anyone']],
Zookeeper::EPHEMERAL
);
} catch (ZookeeperException $ex) {
if (Zookeeper::NODEEXISTS === $ex->getCode()) {
throw new LockConflictedException($ex);
}

throw new LockAcquiringException($ex);
}
}

/**
* Get unique token for store new mutex
*
* @return string
*
* @throws Exception
*/
private function getUniqueToken(): string
{
if (!$this->token) {
$this->token = base64_encode(random_bytes(32));
}

return $this->token;
}
}

0 comments on commit 995e11d

Please sign in to comment.