Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

RateLimiter - shared fixed_window doesn't work as expected #48629

Unanswered
lowwa132 asked this question in Q&A
Discussion options

Hi,

My usecase is to limit my API calls to a rate of 1 per 10 seconds.

Using fixed_window

rate_limiter.yaml:

example:
 policy: 'fixed_window'
 limit: 1
 interval: '10 second'

I'm using a Redis cache pool:

cache.rate_limiter:
 adapter:
 - cache.adapter.redis

The implementation, in a Symfony command:

$limiter = $this->exampleLimiter->create('example');
while (// pagination over) {
 // this blocks the application until the given number of tokens can be consumed
 $limiter->reserve(1)->wait();
 // do the API call
}

In my case, I have 2 pages to retrieve for my API call.
So I have 2 calls per command executed.
If I execute at the same time 4 times my command, the total execution time is:

Command Number Execution time
1 10
2 10
3 20
4 20

In a timelapse of 20 seconds I make 8 API calls. Average rate : 1 per 2.5 seconds.

Using sliding_window

rate_limiter.yaml:

example:
 policy: 'sliding_window'
 limit: 1
 interval: '10 second'

Same cache pool.

The implementation:

$limiter = $this->exampleLimiter->create('example');
while (// pagination over) {
 // can't reserve with sliding_window
 while(!$limiter->consume()->isAccepted()){
 sleep(1);
 }
 // do the API call
}
Command Number Execution time
1 40
2 50
3 60
4 70

In a timelapse of 70 seconds I make 8 API calls. It works as intended.

What am I missing ?

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /