Skip to content

Commit

Permalink
Improve Lock->block method (#52349)
Browse files Browse the repository at this point in the history
* Improve lock->block method to only wait longer if timeout won't be reached shortly.

* Update Lock.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
RedmarBakker and taylorotwell authored Aug 1, 2024
1 parent 8a18838 commit fd9e84e
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Illuminate/Cache/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,18 @@ public function get($callback = null)
*/
public function block($seconds, $callback = null)
{
$starting = $this->currentTime();
$starting = ((int) now()->format('Uu')) / 1000;

$milliseconds = $seconds * 1000;

while (! $this->acquire()) {
Sleep::usleep($this->sleepMilliseconds * 1000);
$now = ((int) now()->format('Uu')) / 1000;

if ($this->currentTime() - $seconds >= $starting) {
if (($now + $this->sleepMilliseconds - $milliseconds) >= $starting) {
throw new LockTimeoutException;
}

Sleep::usleep($this->sleepMilliseconds * 1000);
}

if (is_callable($callback)) {
Expand Down

0 comments on commit fd9e84e

Please sign in to comment.