feat(middleware): optional RateLimiterStoreContext for response headers (#2961)#3007
Open
vishr wants to merge 1 commit into
Open
feat(middleware): optional RateLimiterStoreContext for response headers (#2961)#3007vishr wants to merge 1 commit into
vishr wants to merge 1 commit into
Conversation
…rs (#2961) Adds an optional RateLimiterStoreContext interface. When the configured store implements AllowContext(c, identifier), the rate limiter calls it instead of Allow, giving the store access to the request context so it can set response headers such as Retry-After / X-RateLimit-*. Fully backward compatible: stores implementing only Allow are unchanged. This is the optional-interface approach proposed by the maintainer in the issue thread; it does not alter the existing Allow interface or the built-in store. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
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.
Problem (#2961)
The rate limiter sets no
Retry-After/X-RateLimit-*headers, and theRateLimiterStoreinterface (Allow(identifier) (bool, error)) gives a store no way to set them either.Fix
Add an optional interface:
When the configured store implements it, the middleware calls
AllowContext(with the request context) instead ofAllow, so the store can set response headers on the allow/deny decision.Fully backward compatible — stores implementing only
Alloware unaffected; the existing interface and the built-in memory store are unchanged.This is the optional-interface approach @aldas proposed in the issue thread (mirroring the pattern used in the v4 proxy middleware). It intentionally does not retrofit the built-in store with full metadata plumbing (the part flagged as a larger rewrite in the thread) — it just provides the hook so stores can set headers.
Test
TestRateLimiter_storeAllowContextIsPreferred(written first; fails before the change): a store implementingAllowContextis preferred overAllowand can set aRetry-Afterheader. gofmt/vet clean; full rate-limiter suite passes (backward compat confirmed).Addresses #2961.
🤖 Generated with Claude Code