fix(ratelimit-next): double-count in withRateLimit and broken default rule in middleware#10
Merged
Merged
Conversation
… rule in middleware
withRateLimit called floodgate.check() twice per successful request — once
inside rateLimit() and again to compute headers — halving effective limits.
Fix: inline the check inside withRateLimit so one result covers both the
429 path and header injection.
createRateLimitMiddleware computed the default rule name from
Object.keys({}) which always returns [], so rule selection always fell
back to the literal string "default". If no rule named "default" existed,
floodgate would throw on every request. Fix: expose `rules` on the
Floodgate interface and use Object.keys(floodgate.rules)[0] instead.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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
withRateLimitdouble-count:withRateLimitwas callingfloodgate.check()twice per successful request — once insiderateLimit()and again to compute response headers. Sincecheckruns the algorithm and mutates store counters, every allowed request consumed 2 tokens, effectively halving configured limits."default":createRateLimitMiddlewarecomputed the fallback rule name withObject.keys({} as Record<string, unknown>)[0], which always returnsundefined(empty object), always falling back to the literal string"default". If no rule named"default"was configured, floodgate would throw on every request.Changes
floodgate.ts: Addedreadonly rules: FloodgateConfig["rules"]to theFloodgateinterface and returns it fromcreateFloodgate, so callers can inspect configured rules without casting.next.ts: Inlinedcheck+ header logic insidewithRateLimit(one call, one result, used for both the 429 and success header paths). Fixed middleware rule selection to useObject.keys(floodgate.rules)[0]and use the pre-computedruleNamevariable consistently inside the handler.Test plan
npx turbo run typecheck --filter=ratelimit-nextpassesnpm test— all 302 tests pass, 0 failures🤖 Generated with Claude Code