Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Make throttle lock acquisition retry configurable for concurrency limiter #42242

Merged
merged 1 commit into from
May 3, 2022
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
7 changes: 4 additions & 3 deletions src/Illuminate/Redis/Limiters/ConcurrencyLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ public function __construct($redis, $name, $maxLocks, $releaseAfter)
*
* @param int $timeout
* @param callable|null $callback
* @return bool
* @param int $sleep
* @return mixed
*
* @throws \Illuminate\Contracts\Redis\LimiterTimeoutException
* @throws \Throwable
*/
public function block($timeout, $callback = null)
public function block($timeout, $callback = null, $sleep = 250)
{
$starting = time();

Expand All @@ -74,7 +75,7 @@ public function block($timeout, $callback = null)
throw new LimiterTimeoutException;
}

usleep(250 * 1000);
usleep($sleep * 1000);
}

if (is_callable($callback)) {
Expand Down
22 changes: 21 additions & 1 deletion src/Illuminate/Redis/Limiters/ConcurrencyLimiterBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ class ConcurrencyLimiterBuilder
*/
public $timeout = 3;

/**
* The number of milliseconds to wait between attempts to acquire the lock.
*
* @var int
*/
public $sleep = 250;

/**
* Create a new builder instance.
*
Expand Down Expand Up @@ -96,6 +103,19 @@ public function block($timeout)
return $this;
}

/**
* The number of milliseconds to wait between lock acquisition attempts.
*
* @param int $sleep
* @return $this
*/
public function sleep($sleep)
{
$this->sleep = $sleep;

return $this;
}

/**
* Execute the given callback if a lock is obtained, otherwise call the failure callback.
*
Expand All @@ -110,7 +130,7 @@ public function then(callable $callback, callable $failure = null)
try {
return (new ConcurrencyLimiter(
$this->connection, $this->name, $this->maxLocks, $this->releaseAfter
))->block($this->timeout, $callback);
))->block($this->timeout, $callback, $this->sleep);
} catch (LimiterTimeoutException $e) {
if ($failure) {
return $failure($e);
Expand Down