Skip to content

Commit e7ffd5d

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

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/Symfony/Component/Semaphore/Key.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public function getState(string $stateKey)
8080
return $this->state[$stateKey];
8181
}
8282

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

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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,24 @@ public function testExpiration()
252252
$semaphore = new Semaphore($key, $store);
253253
$this->assertTrue($semaphore->isExpired());
254254
}
255+
256+
/**
257+
* @group time-sensitive
258+
*/
259+
public function testExpirationResetAfter()
260+
{
261+
$store = $this->getMockBuilder(PersistingStoreInterface::class)->getMock();
262+
263+
$key = new Key('key', 1);
264+
$semaphore = new Semaphore($key, $store, 1);
265+
266+
$semaphore->acquire();
267+
$this->assertFalse($semaphore->isExpired());
268+
$semaphore->release();
269+
270+
sleep(2);
271+
272+
$semaphore->acquire();
273+
$this->assertFalse($semaphore->isExpired());
274+
}
255275
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy