10000 Reset Key lifetime time in semaphore · symfony/symfony@128e4ba · GitHub
[go: up one dir, main page]

Skip to content

Commit 128e4ba

Browse files
committed
Reset Key lifetime time in semaphore
1 parent 8c4ecc3 commit 128e4ba

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/Symfony/Component/Semaphore/Key.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ public function getState(string $stateKey)
8181
}
8282

8383
public function reduceLifetime(float $ttlInSeconds)
84+
public function resetLifetime(): void
85+
{
86+
$this->expiringTime = null;
87+
}
88+
8489
{
8590
$newTime = microtime(true) + $ttlInSeconds;
8691

src/Symfony/Component/Semaphore/Semaphore.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function __destruct()
6666
public function acquire(): bool
6767
{
6868
try {
69+
$this->key->resetLifetime();
6970
$this->store->save($this->key, $this->ttlInSecond);
7071
$this->key->reduceLifetime($this->ttlInSecond);
7172
$this->dirty = true;
@@ -97,6 +98,7 @@ public function refresh(?float $ttlInSecond = null)
9798
}
9899

99100
try {
101+
$this->key->resetLifetime();
100102
$this->store->putOffExpiration($this->key, $ttlInSecond);
101103
$this->key->reduceLifetime($ttlInSecond);
102104

src/Symfony/Component/Semaphore/Tests/SemaphoreTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,21 @@ public function testExpiration()
252252
$semaphore = new Semaphore($key, $store);
253253
$this->assertTrue($semaphore->isExpired());
254254
}
255+
256+
public function testExpirationResetAfter()
257+
{
258+
$store = $this->getMockBuilder(PersistingStoreInterface::class)->getMock();
259+
260+
$key = new Key('key', 1);
261+
$semaphore = new Semaphore($key, $store, 0.1);
262+
263+
$semaphore->acquire();
264+
$this->assertFalse($semaphore->isExpired());
265+
$semaphore->release();
266+
267+
\usleep(100001);
268+
269+
$semaphore->acquire();
270+
$this->assertFalse($semaphore->isExpired());
271+
}
255272
}

0 commit comments

Comments
 (0)
0