Skip to content
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

Throttler can access bucket for bucket life time #2074

Merged
merged 7 commits into from
Jun 30, 2019

Conversation

MohKari
Copy link
Contributor

@MohKari MohKari commented Jun 24, 2019

Description
I was encountering an issue with the rate limiting which was causing it not to behave as expected.

If i set a rate limit of 1 request a minute, I would be able to make much more than 1 request a minute provided that the requests are a few seconds apart.

This is because the original "bucket"'s life time gets updated to the time between the 1st and 2nd request, meaning the 3rd request typically creates a new "bucket".

Edit
Added an additional commit to resolve an additional issue regarding calculating how many seconds must pass before a new token is available. (8e0ab84)

Added an additional commit to update ThrottleTest to getTokenTime() to have a value greater than 1 (1eac5c1)

Checklist:

  • Securely signed commits
  • Component(s) with PHPdocs
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@MohKari
Copy link
Contributor Author

MohKari commented Jun 24, 2019

There is a if ($tokens > 0) check performed. This seems to allow a sort of rolling rate limit.

If rate limiting is set to 1 request a minute, you could perform 2 requests back to back, the 3rd request would trigger the "rate limit reached".

Is this intended behaviour? It is easily fixed by changing if ($tokens > 0) to if ($tokens >= 1).

It just seems a bit odd to me to take the token pool below 0.

@MohKari
Copy link
Contributor Author

MohKari commented Jun 24, 2019

Possibly worth noting that if the rate limit is set to 1. You typically get a new bucket by the time you can make a new request, meaning you can make 2 requests every minute. This could be fixed by doubling the amount of time the "buckets" remain in cache.

I'm unsure what kind of effect this would have on performance, which is why i have not added it in myself, which I'm happy to do.

Mohammed Karim added 3 commits June 24, 2019 21:45
…ther CacheHandlers.

Update Throttler to check for null instead of false when looking for existing bucket.
@lonnieezell lonnieezell merged commit 10e689f into codeigniter4:develop Jun 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants