Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@
"require": {
"php": ">=8.1",
"roadrunner-php/lock": "^1.0",
"symfony/lock": "^6.0 || ^7.0"
"symfony/lock": "^6.0 || ^7.0",
"symfony/polyfill-php83": "^1.32"
},
"require-dev": {
"phpunit/phpunit": "^10.0",
"vimeo/psalm": "^5.9"
"vimeo/psalm": "^5.9 || ^6.0"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 1 addition & 0 deletions src/RandomTokenGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function __construct(
) {
}

#[\Override]
public function generate(): string
{
return \bin2hex(\random_bytes($this->length));
Expand Down
22 changes: 17 additions & 5 deletions src/RoadRunnerStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,24 @@ public function __construct(
private readonly RR\LockInterface $lock,
private readonly TokenGeneratorInterface $tokens = new RandomTokenGenerator(),
private readonly float $initialTtl = 300.0,
private readonly float $initialWaitTtl = 60,
private readonly float $initialWaitTtl = 0,
) {
\assert($this->initialTtl >= 0);
\assert($this->initialWaitTtl >= 0);
}

public function withTtl(float $ttl): self
/**
* Clone current instance with another values of ttl.
* @param float $ttl The time-to-live of the lock, in seconds. Defaults to 0 (forever).
* @param float $waitTtl How long to wait to acquire lock until returning false, in seconds.
*/
public function withTtl(float $ttl, ?float $waitTtl = null): self
{
return new self($this->lock, $this->tokens, $ttl, $this->initialWaitTtl);
$waitTtl ??= $this->initialWaitTtl;
return new self($this->lock, $this->tokens, $ttl, $waitTtl);
}

#[\Override]
public function save(Key $key): void
{
\assert(false === $key->hasState(__CLASS__));
Expand All @@ -46,7 +53,7 @@ public function save(Key $key): void
/** @var non-empty-string $resource */
$resource = (string)$key;

$status = $this->lock->lock($resource, $lockId, $this->initialTtl);
$status = $this->lock->lock($resource, $lockId, $this->initialTtl, $this->initialWaitTtl);

if (false === $status) {
throw new LockConflictedException('RoadRunner. Failed to make lock');
Expand All @@ -58,14 +65,15 @@ public function save(Key $key): void
}
}

#[\Override]
public function saveRead(Key $key): void
{
\assert(false === $key->hasState(__CLASS__));
$lockId = $this->getUniqueToken($key);

/** @var non-empty-string $resource */
$resource = (string)$key;
$status = $this->lock->lockRead($resource, $lockId, $this->initialTtl);
$status = $this->lock->lockRead($resource, $lockId, $this->initialTtl, $this->initialWaitTtl);

if (false === $status) {
throw new LockConflictedException('RoadRunner. Failed to make read lock');
Expand All @@ -74,6 +82,7 @@ public function saveRead(Key $key): void
$key->setState(__CLASS__, $lockId);
}

#[\Override]
public function exists(Key $key): bool
{
\assert($key->hasState(__CLASS__));
Expand All @@ -86,6 +95,7 @@ public function exists(Key $key): bool
return $this->lock->exists($resource, $lockId);
}

#[\Override]
public function putOffExpiration(Key $key, float $ttl): void
{
\assert($key->hasState(__CLASS__));
Expand All @@ -101,6 +111,7 @@ public function putOffExpiration(Key $key, float $ttl): void
}
}

#[\Override]
public function delete(Key $key): void
{
\assert($key->hasState(__CLASS__));
Expand All @@ -111,6 +122,7 @@ public function delete(Key $key): void
$this->lock->release($resource, $lockId);
}

#[\Override]
public function waitAndSave(Key $key): void
{
$lockId = $this->getUniqueToken($key);
Expand Down
45 changes: 43 additions & 2 deletions tests/RoadRunnerStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public function testWaitAndSaveSuccess(): void
{
$this->rrLock->expects($this->once())
->method('lock')
->with('resource-name', 'random-id', 300, 60)
->with('resource-name', 'random-id', 300, 0)
->willReturn('lock-id');

$store = new RoadRunnerStore($this->rrLock, $this->tokens);
Expand All @@ -184,10 +184,51 @@ public function testWaitAndSaveFail(): void

$this->rrLock->expects($this->once())
->method('lock')
->with('resource-name', 'random-id', 300, 60)
->with('resource-name', 'random-id', 300, 0)
->willReturn(false);

$store = new RoadRunnerStore($this->rrLock, $this->tokens);
$store->waitAndSave(new Key('resource-name'));
}

/**
* @dataProvider dataWithTtl
*/
public function testWithTtl(float $ttl, ?float $waitTtl, float $ttlExp, float $waitTtlExp): void
{
$this->rrLock->expects($this->once())
->method('lock')
->with('resource-name', 'random-id', $ttlExp, $waitTtlExp)
->willReturn('lock-id');

$s = new RoadRunnerStore($this->rrLock, $this->tokens);
$s->withTtl($ttl, $waitTtl)->save(new Key('resource-name'));
}

/**
* @return iterable
*/
public static function dataWithTtl(): iterable
{
yield [
100,
null,
100,
0,
];

yield [
0.1,
0.1,
0.1,
0.1,
];

yield [
0,
0,
0,
0,
];
}
}
Loading