Skip to content

[RateLimiter] TypeError when using "no_limit" policy #39899

Closed
@Jontsa

Description

@Jontsa

Symfony version(s) affected: 5.2

Description
When using "no_limit" policy with rate limiter, for example during tests, NoLimiter passes \INF as constructor argument to RateLimit which expects integer and this will result in TypeError.

TypeError: Argument 1 passed to Symfony\Component\RateLimiter\RateLimit::__construct() must be of the type int, float given, called in /home/jontsa/Code/demo/vendor/symfony/rate-limiter/Policy/NoLimiter.php on line 37

How to reproduce
Install symfony/rate-limiter component and configure limit using no_limit policy

# config/packages/rate_limiter.yaml
framework:
  rate_limiter:
    anonymous_api:
      policy: 'no_limit'
      limit: 10
      interval: '1 minute'

Then attempt to consume one token using this limiter:

    private function checkAnonymousRateLimit(Request $request, RateLimiterFactory $anonymousApiLimiter) : void
    {
        $limiter = $anonymousApiLimiter->create($request->getClientIp());
        $limit = $limiter->consume(1);
        if (false === $limit->isAccepted()) {
            throw new TooManyRequestsHttpException();
        }
    }

Possible Solution

Additional context

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