Created
January 18, 2019 00:01
-
-
Save mikedfunk/eee84ae45c3ad3d95941c4bb1c5e2fc9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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