8000 [Lock] Expose an expiringDate and isExpired method in Lock by jderusse · Pull Request #22543 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Lock] Expose an expiringDate and isExpired method in Lock #22543

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Expose an expiringDate and isExpired method in Lock
  • Loading branch information
jderusse committed Jul 31, 2017
commit 7d69adc56290d2d950bea1688dd322dc8e1ede2c
24 changes: 24 additions & 0 deletions src/Symfony/Component/Lock/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
final class Key
{
private $resource;
private $expiringDate;
private $state = array();

/**
Expand Down Expand Up @@ -70,4 +71,27 @@ public function getState($stateKey)
{
return $this->state[$stateKey];
}

public function resetExpiringDate()
{
$this->expiringDate = null;
}

/**
* @param \DateTimeImmutable $expiringDate
*/
public function reduceExpiringDate(\DateTimeImmutable $expiringDate)
{
if (null === $this->expiringDate || $this->expiringDate > $expiringDate) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not throw an exception when $this->expiringDate <= $expiringDate?

Copy link
Member Author
@jderusse jderusse May 31, 2017

Choose a reason for hiding this comment

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

I would do that in a method named setExpiringDate. But I didn't want to backport such logic in this class. It purpose it to move foward the expiration date regardless the previous value.

Copy link
Member

Choose a reason for hiding this comment

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

The method name is not clear to me. reduce means that it's going to expire earlier than before, right. But you say that the purpose is to "move forward".

Copy link
Member Author

Choose a reason for hiding this comment

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

When several store are combined, they can define different expiration date, the goal is to take the shorter one. cf if (.. && $this->expiringDate > $expiringDate) { set value}

Indeed, reducing a date does not really make sens. What's about reduceLifeLength or advanceExpiringDate?

10000
Copy link
Member

Choose a reason for hiding this comment

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

It's common to use lifetime or ttl when talking about storage (session, cache, authorizations, ...). Could we use the same wording here? (reduceLifetime could do the job then)

Copy link
Member Author

Choose a reason for hiding this comment

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

Finally replaced reduceExpiringDate(\Datetime $newExpiringDate) by reduceLifetime(float $ttl)

$this->expiringDate = $expiringDate;
}
}

/**
* @return \DateTimeImmutable
Copy link
Member

Choose a reason for hiding this comment

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

\DateTimeImmutable|null

*/
public function getExpiringDate()
{
return $this->expiringDate;
}
}
18 changes: 18 additions & 0 deletions src/Symfony/Component/Lock/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public function refresh()
}

try {
$this->key->resetExpiringDate();
$this->store->putOffExpiration($this->key, $this->ttl);
$this->logger->info('Expiration defined for "{resource}" lock for "{ttl}" seconds.', array('resource' => $this->key, 'ttl' => $this->ttl));
} catch (LockConflictedException $e) {
Expand Down Expand Up @@ -120,4 +121,21 @@ public function release()
throw new LockReleasingException(sprintf('Failed to release the "%s" lock.', $this->key));
}
}

/**
* @return bool
*/
public function isExpired()
{
if (null === $expireDate = $this->key->getExpiringDate()) {
return false;
}

return $expireDate <= new \DateTime();
}

public function getExpiringDate()
{
return $this->key->getExpiringDate();
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Component/Lock/Store/MemcachedStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function save(Key $key)
{
$token = $this->getToken($key);

$key->reduceExpiringDate(\DateTimeImmutable::createFromFormat('U.u', (string) (microtime(true) + $this->initialTtl)));
if ($this->memcached->add((string) $key, $token, (int) ceil($this->initialTtl))) {
return;
}
Expand Down Expand Up @@ -87,6 +88,7 @@ public function putOffExpiration(Key $key, $ttl)

list($value, $cas) = $this->getValueAndCas($key);

$key->reduceExpiringDate(\DateTimeImmutable::createFromFormat('U.u', (string) (microtime(true) + $ttl)));
// Could happens when we ask a putOff after a timeout but in luck nobody steal the lock
if (\Memcached::RES_NOTFOUND === $this->memcached->getResultCode()) {
if ($this->memcached->add((string) $key, $token, $ttl)) {
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Lock/Store/RedisStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public function save(Key $key)
end
';

$expire = (int) ceil($this->initialTtl * 1000);
if (!$this->evaluate($script, (string) $key, array($this->getToken($key), $expire))) {
$key->reduceExpiringDate(\DateTimeImmutable::createFromFormat('U.u', (string) (microtime(true) + $this->initialTtl)));
if (!$this->evaluate($script, (string) $key, array($this->getToken($key), (int) ceil($this->initialTtl * 1000)))) {
throw new LockConflictedException();
}
}
Expand All @@ -81,8 +81,8 @@ public function putOffExpiration(Key $key, $ttl)
end
';

$expire = (int) ceil($ttl * 1000);
if (!$this->evaluate($script, (string) $key, array($this->getToken($key), $expire))) {
$key->reduceExpiringDate(\DateTimeImmutable::createFromFormat('U.u', (string) (microtime(true) + $ttl)));
if (!$this->evaluate($script, (string) $key, array($this->getToken($key), (int) ceil($ttl * 1000)))) {
throw new LockConflictedException();
}
}
Expand Down
30 changes: 30 additions & 0 deletions src/Symfony/Component/Lock/Tests/LockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,34 @@ public function testReleaseThrowsExceptionIfNotWellDeleted()

$lock->release();
}

/**
* @dataProvider provideExpiredDates
*/
public function testExpiration($dates, $expected)
{
$key = new Key(uniqid(__METHOD__, true));
$store = $this->getMockBuilder(StoreInterface::class)->getMock();
$lock = new Lock($key, $store, 10);

foreach ($dates as $date) {
if (null === $date) {
$key->resetExpiringDate();
F438 } else {
$key->reduceExpiringDate(new \DateTimeImmutable($date));
}
}
$this->assertSame($expected, $lock->isExpired());
}

public function provideExpiredDates()
{
yield array(array('yesterday'), true);
yield array(array('tomorrow', 'yesterday'), true);
yield array(array('yesterday', 'tomorrow'), true);

yield array(array(), false);
yield array(array('tomorrow'), false);
yield array(array('yesterday', null), false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,17 @@ public function testSaveWithDifferentResources()
$store->save($key1);
$this->assertTrue($store->exists($key1));
$this->assertFalse($store->exists($key2));
$store->save($key2);

$store->save($key2);
$this->assertTrue($store->exists($key1));
$this->assertTrue($store->exists($key2));

$store->delete($key1);
$this->assertFalse($store->exists($key1));
$this->assertTrue($store->exists($key2));

$store->delete($key2);
$this->assertFalse($store->exists($key1));
$this->assertFalse($store->exists($key2));
}

Expand All @@ -74,7 +77,7 @@ public function testSaveWithDifferentKeysOnSameResources()

try {
$store->save($key2);
throw new \Exception('The store shouldn\'t save the second key');
$this->fail('The store shouldn\'t save the second key');
} catch (LockConflictedException $e) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,16 @@ public function testRefreshLock()
usleep(2.1 * $clockDelay);
$this->assertFalse($store->exists($key));
}

public function testSetExpiration()
{
$key = new Key(uniqid(__METHOD__, true));

/** @var StoreInterface $store */
$store = $this->getStore();

$store->save($key);
$store->putOffExpiration($key, 1);
$this->assertNotNull($key->getExpiringDate());
}
}
0