From 3b1f3286d82d62506c5ba42e9ce4684d2ebce559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Deruss=C3=A9?= Date: Mon, 19 Feb 2018 13:56:49 +0100 Subject: [PATCH] Add a TTL to refresh lock --- src/Symfony/Component/Lock/Lock.php | 11 +++++++---- src/Symfony/Component/Lock/LockInterface.php | 4 +++- src/Symfony/Component/Lock/Tests/LockTest.php | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Lock/Lock.php b/src/Symfony/Component/Lock/Lock.php index 7936a5f7b6d20..f32ad351e3e8e 100644 --- a/src/Symfony/Component/Lock/Lock.php +++ b/src/Symfony/Component/Lock/Lock.php @@ -105,22 +105,25 @@ public function acquire($blocking = false) /** * {@inheritdoc} */ - public function refresh() + public function refresh($ttl = null) { - if (!$this->ttl) { + if (null === $ttl) { + $ttl = $this->ttl; + } + if (!$ttl) { throw new InvalidArgumentException('You have to define an expiration duration.'); } try { $this->key->resetLifetime(); - $this->store->putOffExpiration($this->key, $this->ttl); + $this->store->putOffExpiration($this->key, $ttl); $this->dirty = true; if ($this->key->isExpired()) { throw new LockExpiredException(sprintf('Failed to put off the expiration of the "%s" lock within the specified time.', $this->key)); } - $this->logger->info('Expiration defined for "{resource}" lock for "{ttl}" seconds.', array('resource' => $this->key, 'ttl' => $this->ttl)); + $this->logger->info('Expiration defined for "{resource}" lock for "{ttl}" seconds.', array('resource' => $this->key, 'ttl' => $ttl)); } catch (LockConflictedException $e) { $this->dirty = false; $this->logger->notice('Failed to define an expiration for the "{resource}" lock, someone else acquired the lock.', array('resource' => $this->key)); diff --git a/src/Symfony/Component/Lock/LockInterface.php b/src/Symfony/Component/Lock/LockInterface.php index 75a76426dc5a1..0d1c02ecd8d49 100644 --- a/src/Symfony/Component/Lock/LockInterface.php +++ b/src/Symfony/Component/Lock/LockInterface.php @@ -38,10 +38,12 @@ public function acquire($blocking = false); /** * Increase the duration of an acquired lock. * + * @param float|null $ttl Maximum expected lock duration in seconds + * * @throws LockConflictedException If the lock is acquired by someone else * @throws LockAcquiringException If the lock can not be refreshed */ - public function refresh(); + public function refresh(/* $ttl = null */); /** * Returns whether or not the lock is acquired. diff --git a/src/Symfony/Component/Lock/Tests/LockTest.php b/src/Symfony/Component/Lock/Tests/LockTest.php index 7913644b9a79e..654d58d51c4b6 100644 --- a/src/Symfony/Component/Lock/Tests/LockTest.php +++ b/src/Symfony/Component/Lock/Tests/LockTest.php @@ -97,6 +97,20 @@ public function testRefresh() $lock->refresh(); } + public function testRefreshCustom() + { + $key = new Key(uniqid(__METHOD__, true)); + $store = $this->getMockBuilder(StoreInterface::class)->getMock(); + $lock = new Lock($key, $store, 10); + + $store + ->expects($this->once()) + ->method('putOffExpiration') + ->with($key, 20); + + $lock->refresh(20); + } + public function testIsAquired() { $key = new Key(uniqid(__METHOD__, true)); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy