Skip to content

Commit e984065

Browse files
committed
Fixed possible infinte loop in adaptive sampler
1 parent ebe1a89 commit e984065

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/Sampler/RateLimitingSampler.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function doDecide(int $tracerId, string $operationName): SamplerResult
1919
{
2020
$key = $this->generator->generate($tracerId, $operationName);
2121
$ttl = max((int)(1 / $this->rate + 1), 1);
22-
if (false !== ($current = apcu_add($key, sprintf('%s:%d', time(), 1), $ttl))) {
22+
if (apcu_add($key, sprintf('%s:%d', time(), 1), $ttl)) {
2323
return new SamplerResult(
2424
true, 0x01, [
2525
new SamplerTypeTag('ratelimiting'),
@@ -30,15 +30,17 @@ public function doDecide(int $tracerId, string $operationName): SamplerResult
3030
);
3131
}
3232

33-
while (true) {
33+
$retries = 0;
34+
while ($retries < 5) {
3435
if (false === ($current = apcu_fetch($key))) {
3536
return $this->doDecide($tracerId, $operationName);
3637
}
3738
list ($timestamp, $count) = explode(':', $current);
38-
if ($count / (time() - $timestamp) > $this->rate) {
39+
if ((int)$count / (time() - (int)$timestamp) > $this->rate) {
3940
return new SamplerResult(false, 0);
4041
}
41-
if (false === apcu_cas($key, $current, sprintf('%s:%d', $timestamp, $count + 1))) {
42+
if (false === apcu_cas($key, $current, sprintf('%s:%d', $timestamp, (int)$count + 1))) {
43+
$retries++;
4244
continue;
4345
}
4446

0 commit comments

Comments
 (0)