8000 [Lock] Reset Key lifetime time before we acquire it · symfony/symfony@55ad702 · GitHub
[go: up one dir, main page]

Skip to content

Commit 55ad702

Browse files
committed
[Lock] Reset Key lifetime time before we acquire it
1 parent 1549897 commit 55ad702

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/Symfony/Component/Lock/Lock.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function __destruct()
6767
*/
6868
public function acquire($blocking = false): bool
6969
{
70+
$this->key->resetLifetime();
7071
try {
7172
if ($blocking) {
7273
if (!$this->store instanceof StoreInterface && !$this->store instanceof BlockingStoreInterface) {

src/Symfony/Component/Lock/Tests/LockTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Lock\Key;
1919
use Symfony\Component\Lock\Lock;
2020
use Symfony\Component\Lock\PersistingStoreInterface;
21+
use Symfony\Component\Lock\Store\ExpiringStoreTrait;
2122
use Symfony\Component\Lock\StoreInterface;
2223

2324
/**
@@ -392,4 +393,50 @@ public function provideExpiredDates()
392393
yield [[0.1], false];
393394
yield [[-0.1, null], false];
394395
}
396+
397+
/**
398+
* @group time-sensitive
399+
*/
400+
public function testAcquireTwiceWithExpiration()
401+
{
402+
$key = new Key(uniqid(__METHOD__, true));
403+
$store = new class() implements PersistingStoreInterface {
404+
use ExpiringStoreTrait;
405+
private $keys = [];
406+
private $initialTtl = 30;
407+
408+
public function save(Key $key)
409+
{
410+
$key->reduceLifetime($this->initialTtl);
411+
$this->keys[spl_object_hash($key)] = $key;
412+
$this->checkNotExpired($key);
413+
414+
return true;
415+
}
416+
417+
public function delete(Key $key)
418+
{
419+
unset($this->keys[spl_object_hash($key)]);
420+
}
421+
422+
public function exists(Key $key)
423+
{
424+
return isset($this->keys[spl_object_hash($key)]);
425+
95D6 }
426+
427+
public function putOffExpiration(Key $key, $ttl)
428+
{
429+
$key->reduceLifetime($ttl);
430+
$this->checkNotExpired($key);
431+
}
432+
};
433+
$ttl = 1;
434+
$lock = new Lock($key, $store, $ttl);
435+
436+
$this->assertTrue($lock->acquire());
437+
$lock->release();
438+
sleep($ttl + 1);
439+
$this->assertTrue($lock->acquire());
440+
$lock->release();
441+
}
395442
}

0 commit comments

Comments
 (0)
0