Skip to content

Commit 1873389

Browse files
committed
Avoid division
1 parent e984065 commit 1873389

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/Sampler/RateLimitingSampler.php

Lines changed: 2 additions & 2 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 (apcu_add($key, sprintf('%s:%d', time(), 1), $ttl)) {
22+
if (apcu_add($key, sprintf('%s:%d', microtime(true), 1), $ttl)) {
2323
return new SamplerResult(
2424
true, 0x01, [
2525
new SamplerTypeTag('ratelimiting'),
@@ -36,7 +36,7 @@ public function doDecide(int $tracerId, string $operationName): SamplerResult
3636
return $this->doDecide($tracerId, $operationName);
3737
}
3838
list ($timestamp, $count) = explode(':', $current);
39-
if ((int)$count / (time() - (int)$timestamp) > $this->rate) {
39+
if ($this->rate * (microtime(true) - (float)$timestamp) < (int)$count) {
4040
return new SamplerResult(false, 0);
4141
}
4242
if (false === apcu_cas($key, $current, sprintf('%s:%d', $timestamp, (int)$count + 1))) {

0 commit comments

Comments
 (0)