From d4f516f7faa27ebb2871fa145de51047a8ef080d Mon Sep 17 00:00:00 2001 From: Kamil Dziedzic Date: Sat, 13 Jun 2015 01:34:10 +0200 Subject: [PATCH] Looks like we need separate method --- .../Lock/LockExpirationInterface.php | 6 ++++++ src/NinjaMutex/Lock/MemcacheLock.php | 19 ++++++++++++++++++- src/NinjaMutex/Lock/MemcachedLock.php | 19 ++++++++++++++++++- tests/NinjaMutex/Lock/LockTest.php | 4 +++- 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/NinjaMutex/Lock/LockExpirationInterface.php b/src/NinjaMutex/Lock/LockExpirationInterface.php index eeb8e83..d169acb 100644 --- a/src/NinjaMutex/Lock/LockExpirationInterface.php +++ b/src/NinjaMutex/Lock/LockExpirationInterface.php @@ -20,4 +20,10 @@ interface LockExpirationInterface * @param int $expiration Expiration time of the lock in seconds. */ public function setExpiration($expiration); + + /** + * @param string $name + * @return bool + */ + public function clearLock($name); } diff --git a/src/NinjaMutex/Lock/MemcacheLock.php b/src/NinjaMutex/Lock/MemcacheLock.php index e225d93..b6b2f53 100644 --- a/src/NinjaMutex/Lock/MemcacheLock.php +++ b/src/NinjaMutex/Lock/MemcacheLock.php @@ -61,6 +61,23 @@ public function setExpiration($expiration) $this->expiration = $expiration; } + /** + * Clear lock without releasing it + * Do not use this method unless you know what you do + * + * @param string $name name of lock + * @return bool + */ + public function clearLock($name) + { + if (!isset($this->locks[$name])) { + return false; + } + + unset($this->locks[$name]); + return true; + } + /** * @param string $name name of lock * @param bool $blocking @@ -83,7 +100,7 @@ protected function getLock($name, $blocking) */ public function releaseLock($name) { - if (isset($this->locks[$name]) && ($this->memcache->delete($name) || !$this->isLocked($name))) { + if (isset($this->locks[$name]) && $this->memcache->delete($name)) { unset($this->locks[$name]); return true; diff --git a/src/NinjaMutex/Lock/MemcachedLock.php b/src/NinjaMutex/Lock/MemcachedLock.php index 61cd465..88a2051 100644 --- a/src/NinjaMutex/Lock/MemcachedLock.php +++ b/src/NinjaMutex/Lock/MemcachedLock.php @@ -61,6 +61,23 @@ public function setExpiration($expiration) $this->expiration = $expiration; } + /** + * Clear lock without releasing it + * Do not use this method unless you know what you do + * + * @param string $name name of lock + * @return bool + */ + public function clearLock($name) + { + if (!isset($this->locks[$name])) { + return false; + } + + unset($this->locks[$name]); + return true; + } + /** * @param string $name name of lock * @param bool $blocking @@ -83,7 +100,7 @@ protected function getLock($name, $blocking) */ public function releaseLock($name) { - if (isset($this->locks[$name]) && ($this->memcached->delete($name) || !$this->isLocked($name))) { + if (isset($this->locks[$name]) && $this->memcached->delete($name)) { unset($this->locks[$name]); return true; diff --git a/tests/NinjaMutex/Lock/LockTest.php b/tests/NinjaMutex/Lock/LockTest.php index 7f78180..5249c20 100644 --- a/tests/NinjaMutex/Lock/LockTest.php +++ b/tests/NinjaMutex/Lock/LockTest.php @@ -191,6 +191,8 @@ public function testExpiration(LockFabricWithExpirationInterface $lockFabricWith // Cleanup $this->assertTrue($lockImplementor->releaseLock($name, 0)); - $this->assertTrue($lockImplementorWithExpiration->releaseLock($name, 0)); + // Expired lock is unusable, we need to clean it's lock state or otherwise + // it will invoke in __destruct Exception (php*) or Fatal Error (hhvm) + $this->assertTrue($lockImplementorWithExpiration->clearLock($name, 0)); } }