Skip to content

Instantly share code, notes, and snippets.

@mikedfunk
Created January 18, 2019 00:01
Show Gist options
  • Save mikedfunk/eee84ae45c3ad3d95941c4bb1c5e2fc9 to your computer and use it in GitHub Desktop.
Save mikedfunk/eee84ae45c3ad3d95941c4bb1c5e2fc9 to your computer and use it in GitHub Desktop.
<?php declare(strict_types=1);
namespace MyApp\Adapter\Couchbase;
use CouchbaseException;
/** Releases all couchbase locks that are tracked. */
class LockReleaser
{
// ...
/** @throws \InvalidArgumentException Can only release tracked locks */
public function releaseLocksFromCurrentProcess(): void
{
foreach ($this->lockTracker->getAllTrackingData() as $key => $lockData) {
if ($lockData['type'] !== 'couchbase') {
continue;
}
$this->release($key, $lockData);
$this->lockTracker->stopTracking($key);
}
}
protected function release(string $key, array $lockData): void
{
try {
$this->couchbaseCluster->
openBucket($lockData['bucket_name'])->
unlock($key, ['cas' => $lockData['lock_id']]);
} catch (CouchbaseException $exception) {
if ($exception->getCode() === COUCHBASE_ETMPFAIL) {
// this may just mean the lock is expired
return;
}
throw $exception;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment