Skip to content

Commit

Permalink
feature #26232 [Lock] Add a TTL to refresh lock (jderusse)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.1-dev branch.

Discussion
----------

[Lock] Add a TTL to refresh lock

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | see LockInterface's comment
| Tests pass?   | yes
| Fixed tickets | NA
| License       | MIT
| Doc PR        | NA

Using remote locks in long processes needs to defines a fined grain refresh TTL. For instance, when looping over a long list of jobs we can extends the live of the lock by few seconds before processing each item.
But when the the jobs is splitted and each part to take the same time, we can not define the best TTL

Exemple
```
$lock->acquire();

$this->2minutesJob();

$lock->refresh();
$this->5minutesJob();

$lock->refresh();
$this->1minutesJob();
```

The purpose of this PR is to be able to override the default TTL

```
$lock->acquire();

$lock->refresh(120);
$this->2minutesJob();

$lock->refresh(300);
$this->5minutesJob();

$lock->refresh(60);
$this->1minutesJob();
```

Commits
-------

3b1f328 Add a TTL to refresh lock
  • Loading branch information
fabpot committed Feb 19, 2018
2 parents 3cb5619 + 3b1f328 commit e0bdc0c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/Symfony/Component/Lock/Lock.php
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
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 */);

/**
* Returns whether or not the lock is acquired.
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Lock/Tests/LockTest.php
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

0 comments on commit e0bdc0c

Please sign in to comment.