Repository navigation
fix: use one window for queued rate-limit requests - #67
Open
2748109647 wants to merge 1 commit into
Open
2748109647 wants to merge 1 commit into
2748109647 wants to merge 1 commit into
Conversation
Benchmark Results |
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.
Fixes #66.
Problem
OnLimitcomputes the increment window before acquiring its mutex, whilecalculateRatesamples the clock again after acquiring it. A request queued across a window boundary therefore reads one window and increments another. With the local counter, the stale increment can move the counter backwards and clear both maps, losing counts for other keys. The reset header can also describe the old window.Change
calculateRateAthelper;Statuskeeps sampling its own time through the existing helper.WithLimitCounter. It holds the limiter mutex, queues a request, crosses a real window boundary, seeds another key in the new window, and checks read/write window consistency, the reset header and preservation of that key's count.The public API and sliding-window weighting formula are unchanged.
Validation
Windows/amd64, Go 1.27.1:
go test -run TestOnLimitUsesSameWindowAfterWaitingForLock -count=5 -vpassed in both configurations.go test ./... -count=1passed in the root module, including the final test robustness adjustment.cd _example; go test ./... -count=1passed for the chi integration examples.go vet ./...passed; gofmt andgit diff --checkare clean.The regression test coordinates the mutex directly to reproduce the queued-request schedule with the real local counter. No live Redis backend or load benchmark was run. The race detector was not run because a C compiler is unavailable in this environment.