Repository navigation
fix(rate-limiting): sync periods not longer than sync_rate in realtime - #15037
Open
rajtherengan wants to merge 1 commit into
Open
rajtherengan wants to merge 1 commit into
rajtherengan wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
With
policy = redisandsync_rate >= period(e.g.sync_rate = 1with asecondlimit), counters are not shared across workers and nodes. The first request in a window runsINCRagainst Redis and caches the result per worker; later requests are counted locally and pushed by a timer that firessync_rateseconds later — after the window has already closed. Other workers and nodes never see those increments while they matter, so the effective limit becomes roughlylimit × workers × nodes.This PR implements option 4 from the issue, plus a warning:
<= sync_rateis treated as realtime for that period only: it is counted in Redis on every request, same assync_rate = -1.second+minutewithsync_rate = 1→secondis realtime,minutestays async.I chose this over rejecting the configuration (option 1) so existing declarative configs keep loading on upgrade. Strict validation could still be added in a future major release.
The change is contained to the Redis policy in
kong/plugins/rate-limiting/policies/init.lua: a per-periodis_realtime()check used byusage()andincrement().increment()also skips scheduling a sync timer when every limited period is realtime.Note: this makes the late-sync path in #14998 (
EXPIREATreceiving milliseconds, so recreated keys never expire) much rarer, but does not fix it for async periods. The two changes are independent.Checklist
changelog/unreleased/kongIssue reference
Fixes #15018