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
Prev Previous commit
Replace reduceExpiringDate by reduceLifetime
  • Loading branch information
jderusse committed Aug 29, 2017
commit 566f35eabfddae78bac85e8e3ebe19ba98a52217
20 changes: 11 additions & 9 deletions src/Symfony/Component/Lock/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,23 @@ public function getState($stateKey)
return $this->state[$stateKey];
}

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

/**
* @param \DateTimeImmutable $expiringDate
* @param float $ttl The expiration delay of locks in seconds.
*/
public function reduceExpiringDate(\DateTimeImmutable $expiringDate)
public function reduceLifetime($ttl)
{
if (null === $this->expiringDate || $this->expiringDate > $expiringDate) {
$this->expiringDate = $expiringDate;
$newExpiringDate = \DateTimeImmutable::createFromFormat('U.u', (string) (microtime(true) + $ttl));

if (null === $this->expiringDate || $newExpiringDate < $this->expiringDate) {
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure if it really is expected behaviour that the method silently ignores any argument if it is no smaller than the existing expiration date.

Copy link
Member Author

Choose a reason for hiding this comment

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

IMO it's not an issue if a store ask for a TTL greater than the current one.

$this->expiringDate = $newExpiringDate;
}
}

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

/**
* @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

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

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

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

$key->reduceExpiringDate(\DateTimeImmutable::createFromFormat('U.u', (string) (microtime(true) + $ttl)));
$key->reduceLifetime($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
4 changes: 2 additions & 2 deletions src/Symfony/Component/Lock/Store/RedisStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function save(Key $key)
end
';

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

$key->reduceExpiringDate(\DateTimeImmutable::createFromFormat('U.u', (string) (microtime(true) + $ttl)));
$key->reduceLifetime($ttl);
if (!$this->evaluate($script, (string) $key, array($this->getToken($key), (int) ceil($ttl * 1000)))) {
throw new LockConflictedException();
}
Expand Down
18 changes: 9 additions & 9 deletions src/Symfony/Component/Lock/Tests/LockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,30 +157,30 @@ public function testReleaseThrowsExceptionIfNotWellDeleted()
/**
* @dataProvider provideExpiredDates
*/
public function testExpiration($dates, $expected)
public function testExpiration($ttls, $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) {
foreach ($ttls as $ttl) {
if (null === $ttl) {
$key->resetExpiringDate();
} else {
$key->reduceExpiringDate(new \DateTimeImmutable($date));
$key->reduceLifetime($ttl);
}
}
$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(-1.0), true);
yield array(array(1, -1.0), true);
yield array(array(-1.0, 1), true);

yield array(array(), false);
yield array(array('tomorrow'), false);
yield array(array('yesterday', null), false);
yield array(array(1), false);
yield array(array(-1.0, null), false);
}
}
0