mirror of
https://github.com/johndoe6345789/low-code-react-app-b.git
synced 2026-04-25 14:14:57 +00:00
4.8 KiB
4.8 KiB
Bad Gateway Errors - Fixed
Problem
The application was experiencing masses of "Bad Gateway" (502) errors caused by excessive LLM API calls.
Root Causes Identified
- Auto-scanning running every 2 seconds - The
useAutoRepairhook was automatically scanning all files for errors every 2 seconds, making continuous LLM calls - No rate limiting - Multiple AI features (component generation, code improvement, error repair, etc.) were making unlimited concurrent LLM requests
- No error circuit breaker - Failed requests would retry immediately without backing off
- No request throttling - All AI operations competed for the same gateway resources
Solutions Implemented
1. Rate Limiting System (src/lib/rate-limiter.ts)
- Per-category rate limiting: Different limits for different AI operations
- Time windows: Tracks requests over rolling 60-second windows
- Automatic cleanup: Removes stale tracking data
- Priority queue support: High-priority requests can retry with backoff
- Status tracking: Monitor remaining capacity and reset times
Configuration:
- AI Operations: Max 3 requests per minute
- Error Scanning: Max 1 request per 30 seconds
2. Protected LLM Service (src/lib/protected-llm-service.ts)
- Error tracking: Monitors consecutive failures
- Circuit breaker: Pauses all requests after 5 consecutive errors
- User-friendly error messages: Converts technical errors to actionable messages
- Automatic recovery: Error count decreases on successful calls
- Request categorization: Groups related operations for better rate limiting
3. Disabled Automatic Scanning
- Removed automatic useEffect trigger in
useAutoRepair - Manual scanning only: Users must explicitly click "Scan" button
- Rate-limited when triggered: Even manual scans respect rate limits
4. Updated All AI Services
- ai-service.ts: All methods now use
ProtectedLLMService - error-repair-service.ts: Code repair uses rate limiting
- Consistent error handling: All services handle 502/429 errors gracefully
Benefits
- No more cascading failures: Rate limiting prevents overwhelming the gateway
- Better user experience: Clear error messages explain what went wrong
- Automatic recovery: Circuit breaker allows system to recover from issues
- Resource efficiency: Prevents wasted requests that would fail anyway
- Predictable behavior: Users understand when operations might be delayed
How It Works Now
Normal Operation
- User triggers an AI feature (generate component, improve code, etc.)
- Request goes through
ProtectedLLMService - Rate limiter checks if request is allowed
- If allowed, request proceeds
- If rate-limited, user sees friendly message about slowing down
Error Handling
-
If LLM call fails with 502/Bad Gateway:
- User sees: "Service temporarily unavailable - please wait a moment"
- Error count increases
- Request is blocked by rate limiter for the category
-
If too many consecutive errors (5+):
- Circuit breaker trips
- All AI operations pause
- User sees: "AI service temporarily unavailable due to repeated errors"
-
Recovery:
- Successful requests decrease error count
- After error count drops, circuit breaker resets
- Normal operation resumes
Manual Controls
Users can check AI service status:
const stats = ProtectedLLMService.getStats()
// Returns: { totalCalls, errorCount, isPaused }
Users can manually reset if needed:
ProtectedLLMService.reset()
// Clears all rate limits and error counts
Testing the Fix
- Verify no automatic scanning: Open the app - no LLM calls should fire automatically
- Test rate limiting: Try generating 5 components quickly - should see rate limit message
- Test error recovery: If you hit an error, next successful call should work
- Check manual scan: Error panel scan button should work with rate limiting
Monitoring
Watch the browser console for:
LLM call failed (category): error- Individual failuresRate limit exceeded for llm-category- Rate limiting in actionToo many LLM errors detected- Circuit breaker activation
Future Improvements
- Retry queue: Queue rate-limited requests and auto-retry
- Progressive backoff: Increase delays after repeated failures
- Request deduplication: Prevent identical simultaneous requests
- Usage analytics: Track which features use most AI calls
- User quotas: Per-user rate limiting for multi-tenant deployments
Files Modified
/src/lib/rate-limiter.ts(NEW)/src/lib/protected-llm-service.ts(NEW)/src/lib/ai-service.ts(UPDATED - now uses rate limiting)/src/lib/error-repair-service.ts(UPDATED - now uses rate limiting)/src/hooks/use-auto-repair.ts(UPDATED - disabled automatic scanning)