@@ -19,7 +19,7 @@ public function doDecide(int $tracerId, string $operationName): SamplerResult
19
19
{
20
20
$ key = $ this ->generator ->generate ($ tracerId , $ operationName );
21
21
$ 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 )) {
23
23
return new SamplerResult (
24
24
true , 0x01 , [
25
25
new SamplerTypeTag ('ratelimiting ' ),
@@ -30,15 +30,17 @@ public function doDecide(int $tracerId, string $operationName): SamplerResult
30
30
);
31
31
}
32
32
33
- while (true ) {
33
+ $ retries = 0 ;
34
+ while ($ retries < 5 ) {
34
35
if (false === ($ current = apcu_fetch ($ key ))) {
35
36
return $ this ->doDecide ($ tracerId , $ operationName );
36
37
}
37
38
list ($ timestamp , $ count ) = explode (': ' , $ current );
38
- if ($ count / (time () - $ timestamp ) > $ this ->rate ) {
39
+ if (( int ) $ count / (time () - ( int ) $ timestamp ) > $ this ->rate ) {
39
40
return new SamplerResult (false , 0 );
40
41
}
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 ++;
42
44
continue ;
43
45
}
44
46
0 commit comments