Skip to content

Commit

Permalink
separated wait logic into public function
Browse files Browse the repository at this point in the history
  • Loading branch information
matthi4s committed Nov 12, 2021
1 parent 2d6434f commit cfe4430
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ public function __construct(string $key)
*/
public function lock(bool $exclusive = false, int $time = 120, int $wait = 300, string $identifier = null): bool
{
$startTime = time();
$this->exclusive = $exclusive;
$this->time = $time;

Expand All @@ -278,15 +277,10 @@ public function lock(bool $exclusive = false, int $time = 120, int $wait = 300,
$this->identifier = $identifier;
}

$this->update();
$this->retries = 0;

do {
while (!$this->canLock() && $startTime + $wait > time()) {
sleep(static::$waitRetryInterval);
$this->update();
}

$this->waitForOtherLocks($wait, $exclusive);
$retry = false;
if ($this->canLock()) {
$retry = !$this->addOrUpdateLock();
Expand All @@ -296,6 +290,26 @@ public function lock(bool $exclusive = false, int $time = 120, int $wait = 300,
return !!$this->isLocked();
}

/**
* @param int $wait
* @param bool $exclusive
* @return bool
* @throws InvalidResponseStatusCodeException
*/
public function waitForOtherLocks(int $wait = 300, bool $exclusive = false): bool
{
$startTime = time();
$this->exclusive = $exclusive;
$this->update();

while (!$this->canLock() && $startTime + $wait > time()) {
sleep(static::$waitRetryInterval);
$this->update();
}

return $this->canLock();
}

/**
* Check if is locked and returns time until lock runs out or false
*
Expand Down

0 comments on commit cfe4430

Please sign in to comment.