Reset lifetime on acquireRead()
This commit is contained in:
parent
8fa0573ab6
commit
de412bf24b
@ -121,6 +121,7 @@ final class Lock implements SharedLockInterface, LoggerAwareInterface
|
|||||||
*/
|
*/
|
||||||
public function acquireRead(bool $blocking = false): bool
|
public function acquireRead(bool $blocking = false): bool
|
||||||
{
|
{
|
||||||
|
$this->key->resetLifetime();
|
||||||
try {
|
try {
|
||||||
if (!$this->store instanceof SharedLockStoreInterface) {
|
if (!$this->store instanceof SharedLockStoreInterface) {
|
||||||
$this->logger->debug('Store does not support ReadLocks, fallback to WriteLock.', ['resource' => $this->key]);
|
$this->logger->debug('Store does not support ReadLocks, fallback to WriteLock.', ['resource' => $this->key]);
|
||||||
|
@ -20,6 +20,7 @@ use Symfony\Component\Lock\Key;
|
|||||||
use Symfony\Component\Lock\Lock;
|
use Symfony\Component\Lock\Lock;
|
||||||
use Symfony\Component\Lock\PersistingStoreInterface;
|
use Symfony\Component\Lock\PersistingStoreInterface;
|
||||||
use Symfony\Component\Lock\SharedLockStoreInterface;
|
use Symfony\Component\Lock\SharedLockStoreInterface;
|
||||||
|
use Symfony\Component\Lock\Store\ExpiringStoreTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Jérémy Derussé <jeremy@derusse.com>
|
* @author Jérémy Derussé <jeremy@derusse.com>
|
||||||
@ -429,6 +430,52 @@ class LockTest extends TestCase
|
|||||||
$this->assertTrue($lock->acquireRead(false));
|
$this->assertTrue($lock->acquireRead(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group time-sensitive
|
||||||
|
*/
|
||||||
|
public function testAcquireReadTwiceWithExpiration()
|
||||||
|
{
|
||||||
|
$key = new Key(uniqid(__METHOD__, true));
|
||||||
|
$store = new class() implements PersistingStoreInterface {
|
||||||
|
use ExpiringStoreTrait;
|
||||||
|
private $keys = [];
|
||||||
|
private $initialTtl = 30;
|
||||||
|
|
||||||
|
public function save(Key $key)
|
||||||
|
{
|
||||||
|
$key->reduceLifetime($this->initialTtl);
|
||||||
|
$this->keys[spl_object_hash($key)] = $key;
|
||||||
|
$this->checkNotExpired($key);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function delete(Key $key)
|
||||||
|
{
|
||||||
|
unset($this->keys[spl_object_hash($key)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function exists(Key $key)
|
||||||
|
{
|
||||||
|
return isset($this->keys[spl_object_hash($key)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function putOffExpiration(Key $key, float $ttl)
|
||||||
|
{
|
||||||
|
$key->reduceLifetime($ttl);
|
||||||
|
$this->checkNotExpired($key);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
$ttl = 1;
|
||||||
|
$lock = new Lock($key, $store, $ttl);
|
||||||
|
|
||||||
|
$this->assertTrue($lock->acquireRead());
|
||||||
|
$lock->release();
|
||||||
|
sleep($ttl + 1);
|
||||||
|
$this->assertTrue($lock->acquireRead());
|
||||||
|
$lock->release();
|
||||||
|
}
|
||||||
|
|
||||||
public function testAcquireReadBlockingWithBlockingSharedLockStoreInterface()
|
public function testAcquireReadBlockingWithBlockingSharedLockStoreInterface()
|
||||||
{
|
{
|
||||||
$key = new Key(uniqid(__METHOD__, true));
|
$key = new Key(uniqid(__METHOD__, true));
|
||||||
|
Reference in New Issue
Block a user