fix(proxy): atomic Durable Object rate limiting#151
Open
zhongnansu wants to merge 1 commit into
Open
Conversation
Replace eventually-consistent KV counters (read-then-write race let concurrent requests bypass the perMinute/perDay caps) with a Cloudflare Durable Object that does check-and-increment inside blockConcurrencyWhile. - Per-IP DO holds minute/day/burst counters - Service-wide global cap routed through a single dedicated `__global__` DO instance via one atomic /global/check-increment endpoint (no cross-DO TOCTOU); fail-closed on binding/fetch/parse errors - Lower globalPerDay 5000 -> 500 as a cost backstop (real backstop is an upstream OpenAI account spend cap) - Prune previous-window bucket keys to bound DO storage growth Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
📦 PR Preview — Ask AI ExtensionVersion: PermissionsNo permission changes detected. ✅ Preview ChecklistBefore merging, manually test with the artifact above:
Updated by PR Preview Bot — workflow run |
Contributor
📊 Code Coverage Report
Threshold: 80% ✅ Coverage meets the minimum threshold. Updated by Code Coverage workflow |
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
The proxy's rate limiting used Cloudflare KV, which is eventually consistent. Concurrent requests all read the same low count and passed the cap before any increment landed — so
perMinute/perDay/globalPerDaywere not actually enforced (cost / denial-of-wallet risk).Fix
blockConcurrencyWhile(serialized, strongly consistent).__global__DO instance via one atomic/global/check-incrementendpoint — decision + mutation in one serialized gate, so there's no cross-DO TOCTOU. Fail-closed on missing binding / fetch error / malformed response.globalPerDay5000 → 500 as a cost backstop.Testing
__global__DO, and asserts exactly one is allowed (would fail against the old split check/increment design).node --checkclean on all changed sources.Deploy note
Not deployed.
wrangler.tomladds theRateLimiterDO binding +new_sqlite_classesmigration; deploying will requirewrangler deploy.🤖 Generated with Claude Code