Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DaRealFreak opened this issue Dec 5, 2022 · 0 comments · Fixed by #48501
Closed

Comments

@DaRealFreak
Copy link
Contributor

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

@DaRealFreak DaRealFreak added the Bug label Dec 5, 2022
nicolas-grekas added a commit that referenced this issue Dec 9, 2022
…eak)

This PR was submitted for the 6.3 branch but it was merged into the 5.4 branch instead.

Discussion
----------

[RateLimiter] Add `int` to `Reservation::wait()`

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #48500
| License       | MIT

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)
To prevent deprecation notices we can typecast the float value to int before passing it to `usleep`

Commits
-------

6ad3279 [RateLimiter] Add typecast to Reservation::wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants