Skip to content

Commit fcb93a2

Browse files
Change the Loop to have a minimum wait time of 1 milli second and a maximum wait time of 1 second.
1 parent d5e9996 commit fcb93a2

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

classes/util/Loop.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414
*/
1515
class Loop
1616
{
17+
18+
/**
19+
* Minimum time that we want to wait, between lock checks.
20+
*
21+
* In micro seconds.
22+
*/
23+
private const MINIMUM_WAIT_US = 1e4;
24+
25+
/**
26+
* Maximum time that we want to wait, between lock checks.
27+
*
28+
* In micro seconds.
29+
*/
30+
private const MAXIMUM_WAIT_US = 1e6;
1731

1832
/**
1933
* @var int The timeout in seconds.
@@ -69,7 +83,6 @@ public function execute(callable $code)
6983
{
7084
$this->looping = true;
7185

72-
$minWait = 100; // microseconds
7386
$deadline = microtime(true) + $this->timeout; // At this time, the lock will time out.
7487
$result = null;
7588

@@ -79,9 +92,6 @@ public function execute(callable $code)
7992
break;
8093
}
8194

82-
$min = (int) $minWait * 1.5 ** $i;
83-
$max = $min * 2;
84-
8595
/*
8696
* Calculate max time remaining, don't sleep any longer than that.
8797
*/
@@ -94,9 +104,12 @@ public function execute(callable $code)
94104
throw TimeoutException::create($this->timeout);
95105
}
96106

97-
$usleep = \min($usecRemaining, \random_int($min, $max));
107+
$min = min((int) self::MINIMUM_WAIT_US * 1.5 ** $i, self::MAXIMUM_WAIT_US);
108+
$max = min($min * 2, self::MAXIMUM_WAIT_US);
109+
110+
$usecToSleep = \min($usecRemaining, \random_int($min, $max));
98111

99-
usleep($usleep);
112+
usleep($usecToSleep);
100113
}
101114

102115
if (microtime(true) >= $deadline) {

0 commit comments

Comments
 (0)