-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Lock] Check TTL expiration in lock acquisition #22542
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Lock\Exception; | ||
|
||
/** | ||
* LockExpiredException is thrown when a lock may conflict due to a TTL expiration. | ||
* | ||
* @author Jérémy Derussé <jeremy@derusse.com> | ||
*/ | ||
class LockExpiredException extends \RuntimeException implements ExceptionInterface | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
final class Key | ||
{ | ||
private $resource; | ||
private $expiringDate; | ||
private $expiringTime; | ||
private $state = array(); | ||
|
||
/** | ||
|
@@ -72,28 +72,38 @@ public function getState($stateKey) | |
return $this->state[$stateKey]; | ||
} | ||
|
||
public function resetLifetime() | ||
{ | ||
$this->expiringTime = null; | ||
} | ||
|
||
/** | ||
* @param float $ttl The expiration delay of locks in seconds. | ||
*/ | ||
public function reduceLifetime($ttl) | ||
{ | ||
$newExpiringDate = \DateTimeImmutable::createFromFormat('U.u', (string) (microtime(true) + $ttl)); | ||
$newTime = microtime(true) + $ttl; | ||
|
||
if (null === $this->expiringDate || $newExpiringDate < $this->expiringDate) { | ||
$this->expiringDate = $newExpiringDate; | ||
if (null === $this->expiringTime || $this->expiringTime > $newTime) { | ||
$this->expiringTime = $newTime; | ||
} | ||
} | ||
|
||
public function resetExpiringDate() | ||
/** | ||
* Returns the remaining lifetime. | ||
* | ||
* @return float|null Remaining lifetime in seconds. Null when the key won't expire. | ||
*/ | ||
public function getRemainingLifetime() | ||
{ | ||
$this->expiringDate = null; | ||
return null === $this->expiringTime ? null : $this->expiringTime - microtime(true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should it be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd keep null with a comment explaining its meaning in the return annotation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
} | ||
|
||
/** | ||
* @return \DateTimeImmutable | ||
* @return bool | ||
*/ | ||
public function getExpiringDate() | ||
public function isExpired() | ||
{ | ||
return $this->expiringDate; | ||
return null !== $this->expiringTime && $this->expiringTime <= microtime(true); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switch to float comparison instead of datetime, due to bug in PHP => https://3v4l.org/bSt3d