Skip to content

[Lock] Add a TTL to refresh lock #26232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/Symfony/Component/Lock/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Lock/LockInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not such this is the right way to keep BC layer on interfaces. ping @nicolas-grekas

Is there a way to trigger a deprecation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can trigger a deprecation in the interface like e8351d8#diff-7994cccf0d9c75a5a481e1240a159cce


/**
* Returns whether or not the lock is acquired.
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Lock/Tests/LockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down