Skip to content

[RateLimiter] Random reservation duration #47103

Closed
@TCM-dev

Description

@TCM-dev

Symfony version(s) affected

6.0.10

Description

Original issue I was having was because I was not calling the consume method after the reserve/wait methods.
This may seem obvious but I believe documentation could be improved by adding one line with the consume method under the reserve/wait examples. Does that deserve an issue/PR on its own ?

However there is still a bug

Actual issue

Let's say I want to implement the symfony rate limiter for a batch process.
I have 1000 things to process, chunked in batches of 5.

If I configure my rate limiter as follow

framework:
  rate_limiter:
    worker_requests:
      policy: 'token_bucket'
      limit: 5
      rate: { interval: '1 second', amount: 1 }

I am expecting to wait 0s for the first batch, then wait 5s for each others.
However, from time to time, I can immediately reserve tokens when it should not be the case

Possible Solution

No response

Additional Context

To reproduce

        $array = range(0, 1000);
        $chunks = array_chunk($array, 5);

        foreach ($chunks as $chunk) {
            $stopwatch->start('reservation');

            $reservation = $limiter->reserve(count($chunk));
            $reservation->wait();
            $limiter->consume(count($chunk));

            $event = $stopwatch->stop('reservation');
            $duration = $event->getDuration();
            $stopwatch->reset();
            dump("Waited $duration milliseconds");
        }

Output from my test command with the given configuration/snippet of code
image

The problem is not occuring when manually using a new RateLimiterFactory with an InMeMoryStorage

        $factory = new RateLimiterFactory([
            'id' => 'login',
            'policy' => 'token_bucket',
            'limit' => 5,
            'rate' => ['interval' => '1 second'],
        ], new InMemoryStorage());

        $limiter = $factory->create('command');

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