8000 Add a TTL to refresh lock · symfony/symfony@ded0225 · GitHub
[go: up one dir, main page]

Skip to content

Commit ded0225

Browse files
committed
Add a TTL to refresh lock
1 parent 3303355 commit ded0225

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

src/Symfony/Component/Lock/Lock.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,25 @@ public function acquire($blocking = false)
105105
/**
106106
* {@inheritdoc}
107107
*/
108-
public function refresh()
108+
public function refresh($ttl = null)
109109
{
110-
if (!$this->ttl) {
110+
if (null === $ttl) {
111+
$ttl = $this->ttl;
112+
}
113+
if (!$ttl) {
111114
throw new InvalidArgumentException('You have to define an expiration duration.');
112115
}
113116

114117
try {
115118
$this->key->resetLifetime();
116-
$this->store->putOffExpiration($this->key, $this->ttl);
119+
$this->store->putOffExpiration($this->key, $ttl);
117120
$this->dirty = true;
118121

119122
if ($this->key->isExpired()) {
120123
throw new LockExpiredException(sprintf('Failed to put off the expiration of the "%s" lock within the specified time.', $this->key));
121124
}
122125

123-
$this->logger->info('Expiration defined for "{resource}" lock for "{ttl}" seconds.', array('resource' => $this->key, 'ttl' => $this->ttl));
126+
$this->logger->info('Expiration defined for "{resource}" lock for "{ttl}" seconds.', array('resource' => $this->key, 'ttl' => $ttl));
124127
} catch (LockConflictedException $e) {
125128
$this->dirty = false;
126129
$this->logger->notice('Failed to define an expiration for the "{resource}" lock, someone else acquired the lock.', array('resource' => $this->key));

src/Symfony/Component/Lock/LockInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ public function acquire($blocking = false);
3838
/**
3939
* Increase the duration of an acquired lock.
4040
*
41+
* @param float|null $ttl Maximum expected lock duration in seconds.
42+
*
4143
* @throws LockConflictedException If the lock is acquired by someone else
4244
* @throws LockAcquiringException If the lock can not be refreshed
4345
*/
44-
public function refresh();
46+
public function refresh($ttl = null);
4547

4648
/**
4749
* Returns whether or not the lock is acquired.

src/Symfony/Component/Lock/Tests/LockTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ public function testRefresh()
9797
$lock->refresh();
9898
}
9999

100+
public function testRefreshCustom()
101+
{
102+
$key = new Key(uniqid(__METHOD__, true));
103+
$store = $this->getMockBuilder(StoreInterface::class)->getMock();
104+
$lock = new Lock($key, $store, 10);
105+
106+
$store
107+
->expects($this->once())
108+
->method('putOffExpiration')
109+
->with($key, 20);
110+
111+
$lock->refresh(20);
112+
}
113+
100114
public function testIsAquired()
101115
{
102116
$key = new Key(uniqid(__METHOD__, true));

0 commit comments

Comments
 (0)
0