Skip to content

[RateLimiter] Implicit conversion from float to int losing precision in Reservation.php #48500

@DaRealFreak

Description

@DaRealFreak

Symfony version(s) affected

6.2.0

Description

I'm receiving following errors in my application
PHP Deprecation Notice: Implicit conversion from float 59999985.93330383 to int loses precision in /var/www/html/vendor/symfony/rate-limiter/Reservation.php line 48

The usleep function accepts int as parameter type (https://www.php.net/manual/en/function.usleep.php), while the passed type is float in Reservation::wait (due to Reservation::getWaitDuration returning a float value for the 1e6 multiplication)

How to reproduce

$limiter = (new RateLimiterFactory(
    [
        'id' => 'api',
        'policy' => 'token_bucket',
        'rate' => [
            'interval' => '1 minute',
            'amount' => 1
        ],
        'limit' => 1,
    ],
    new InMemoryStorage()
))->create('api');

$limiter->reserve()->wait();
$limiter->reserve()->wait();

Possible Solution

type casting the resulting wait time from float to int fixes the problem
usleep((int) ($this->getWaitDuration() * 1e6));

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions