Skip to content
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

[Semaphore] Reset Key lifetime time before we acquire it #38581

Merged
merged 1 commit into from Oct 15, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Semaphore/Key.php
Expand Up @@ -80,6 +80,11 @@ public function getState(string $stateKey)
return $this->state[$stateKey];
}

public function resetLifetime(): void
{
$this->expiringTime = null;
}

public function reduceLifetime(float $ttlInSeconds)
{
$newTime = microtime(true) + $ttlInSeconds;
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Semaphore/Semaphore.php
Expand Up @@ -66,6 +66,7 @@ public function __destruct()
public function acquire(): bool
{
try {
$this->key->resetLifetime();
$this->store->save($this->key, $this->ttlInSecond);
$this->key->reduceLifetime($this->ttlInSecond);
$this->dirty = true;
Expand Down Expand Up @@ -97,6 +98,7 @@ public function refresh(?float $ttlInSecond = null)
}

try {
$this->key->resetLifetime();
$this->store->putOffExpiration($this->key, $ttlInSecond);
$this->key->reduceLifetime($ttlInSecond);

Expand Down
20 changes: 20 additions & 0 deletions src/Symfony/Component/Semaphore/Tests/SemaphoreTest.php
Expand Up @@ -252,4 +252,24 @@ public function testExpiration()
$semaphore = new Semaphore($key, $store);
$this->assertTrue($semaphore->isExpired());
}

jderusse marked this conversation as resolved.
Show resolved Hide resolved
/**
* @group time-sensitive
*/
public function testExpirationResetAfter()
{
$store = $this->getMockBuilder(PersistingStoreInterface::class)->getMock();

$key = new Key('key', 1);
$semaphore = new Semaphore($key, $store, 1);

$semaphore->acquire();
$this->assertFalse($semaphore->isExpired());
$semaphore->release();

sleep(2);

$semaphore->acquire();
$this->assertFalse($semaphore->isExpired());
}
}