Skip to content

Commit 0b4f74b

Browse files
[9.x] Make throttle lock acquisition retry time configurable (#41516)
* Make throttle lock acquisition attempt retry time configurable Depending on context, 750ms can be a long time to wait between retries for a throttle. We are making the retry time configurable so that users can tune the throttling further. All changes are backwards compatible. I set the unit as milliseconds because this seemed like the natural time unit to use. Seconds would require floats which felt dirty. That being said, it's not consistent with the unit used for `->every` and `->block` on the builder. So I'm open to changing it for consistency. ```php Redis::throttle('my-key') ->allow(5) ->every(10) ->retryWait(100) ->then(static fn () => echo "hello!"); ``` * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent 4b202ba commit 0b4f74b

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/Illuminate/Redis/Limiters/DurationLimiter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,12 @@ public function __construct($redis, $name, $maxLocks, $decay)
7070
*
7171
* @param int $timeout
7272
* @param callable|null $callback
73+
* @param int $sleep
7374
* @return mixed
7475
*
7576
* @throws \Illuminate\Contracts\Redis\LimiterTimeoutException
7677
*/
77-
public function block($timeout, $callback = null)
78+
public function block($timeout, $callback = null, $sleep = 750)
7879
{
7980
$starting = time();
8081

@@ -83,7 +84,7 @@ public function block($timeout, $callback = null)
8384
throw new LimiterTimeoutException;
8485
}
8586

86-
usleep(750 * 1000);
87+
usleep($sleep * 1000);
8788
}
8889

8990
if (is_callable($callback)) {

src/Illuminate/Redis/Limiters/DurationLimiterBuilder.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ class DurationLimiterBuilder
4444
*/
4545
public $timeout = 3;
4646

47+
/**
48+
* The number of milliseconds to wait between attempts to acquire the lock.
49+
*
50+
* @var int
51+
*/
52+
public $sleep = 750;
53+
4754
/**
4855
* Create a new builder instance.
4956
*
@@ -96,6 +103,19 @@ public function block($timeout)
96103
return $this;
97104
}
98105

106+
/**
107+
* The number of milliseconds to wait between lock acquisition attempts.
108+
*
109+
* @param int $sleep
110+
* @return $this
111+
*/
112+
public function sleep($sleep)
113+
{
114+
$this->sleep = $sleep;
115+
116+
return $this;
117+
}
118+
99119
/**
100120
* Execute the given callback if a lock is obtained, otherwise call the failure callback.
101121
*
@@ -110,7 +130,7 @@ public function then(callable $callback, callable $failure = null)
110130
try {
111131
return (new DurationLimiter(
112132
$this->connection, $this->name, $this->maxLocks, $this->decay
113-
))->block($this->timeout, $callback);
133+
))->block($this->timeout, $callback, $this->sleep);
114134
} catch (LimiterTimeoutException $e) {
115135
if ($failure) {
116136
return $failure($e);

0 commit comments

Comments
 (0)