Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"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]
Comment thread
gam6itko marked this conversation as resolved.
public function generate(): string
{
return \bin2hex(\random_bytes($this->length));
Expand Down
16 changes: 11 additions & 5 deletions src/RoadRunnerStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ 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
public function withTtl(float $ttl, float $waitTtl = 0): self
Comment thread
gam6itko marked this conversation as resolved.
Outdated
{
return new self($this->lock, $this->tokens, $ttl, $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 +47,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 +59,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 +76,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 +89,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 +105,7 @@ public function putOffExpiration(Key $key, float $ttl): void
}
}

#[\Override]
public function delete(Key $key): void
{
\assert($key->hasState(__CLASS__));
Expand All @@ -111,6 +116,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
4 changes: 2 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,7 +184,7 @@ 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);
Expand Down
Loading