You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: Add retry logic and fail-safe rate limiting to RateLimiterDO
## Problem
Rate limiting was failing under high load when transaction conflicts occurred.
The critical bug: when transactionSync() failed, the code returned "ok" status,
which completely bypassed rate limiting. This allowed unlimited requests through
during high concurrency scenarios.
## Root Cause
Location: RateLimiterDO.ts:299-314 (before this fix)
When database transactions failed due to:
- High concurrency (many requests hitting same bucket)
- Transaction timeouts
- Database lock contention
The error handler returned:
```
status: "ok", // ⚠️ BYPASSES RATE LIMITING!
remaining: req.quota,
currentUsage: 0
```
This meant ALL failed requests were allowed through, defeating the entire
purpose of rate limiting.
## The Fix
### 1. Retry Logic
- Try transaction up to 3 times before giving up
- Exponential backoff: 10ms, 20ms, 30ms delays
- Reduces transient failures from contention
- Gives transactions time to complete
### 2. Fail-Safe Rate Limiting (CRITICAL)
Changed error response from "ok" to "rate_limited":
```
status: "rate_limited", // ✅ FAIL-SAFE: Deny rather than allow
remaining: 0,
currentUsage: req.quota
```
## Why This Matters
### Security
- Prevents bypassing rate limits via high load
- Protects against abuse and runaway API costs
- Bad actors can't exploit transaction failures
### Reliability
- System degrades gracefully under load
- Better to deny a few legitimate requests than allow unlimited throughput
- Maintains rate limit guarantees even during failures
## Testing
- ✅ TypeScript compilation passes
- ✅ Worker starts successfully with new code
- ✅ Backward compatible - no API changes
## Impact
- Fixes rate limiting failures reported by users with:
- High rate limits ($100)
- Many small requests (~$0.0001 each)
- High volume (1M+ requests in time window)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
0 commit comments