Repository navigation
fix(rate-limit): prevent endless queue with maxWait (#297) - #301
Conversation
…#297) When all provider quotas are exhausted (reservoir=0 after repeated 429s), Bottleneck's schedule() would queue requests indefinitely since no maxWait was configured. Clients (Cursor, Claude Code, VS Code) would hang forever. Fix: add maxWait=120000 (2min, configurable via RATE_LIMIT_MAX_WAIT_MS env) to DEFAULT_SETTINGS and all three Bottleneck constructors. When a job waits longer than maxWait, Bottleneck rejects with a BottleneckError which propagates as a 502/503 error to the client — a clean fail-fast instead of infinite hang.
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical issue where requests could become stuck in an endless queue when rate limits were hit across all providers. By implementing a configurable maximum wait time for queued jobs, the system now gracefully handles rate limit exhaustion, ensuring that requests either proceed within a defined timeframe or fail with a clear error, thereby improving client experience and system stability. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
| // Max time (ms) a job can wait in queue before failing with a timeout error. | ||
| // Prevents infinite queuing when all providers are exhausted after a 429. | ||
| // Configurable via RATE_LIMIT_MAX_WAIT_MS env var (default: 2 minutes). | ||
| const MAX_WAIT_MS = parseInt(process.env.RATE_LIMIT_MAX_WAIT_MS || "120000", 10); |
There was a problem hiding this comment.
WARNING: Missing validation for invalid or negative values — parseInt returns NaN for non-numeric strings (e.g., RATE_LIMIT_MAX_WAIT_MS=abc), and negative values would be invalid timeouts. Consider adding validation:
const MAX_WAIT_MS = Math.max(0, parseInt(process.env.RATE_LIMIT_MAX_WAIT_MS || "120000", 10));
Code Review SummaryStatus: 1 Issue Found | Recommendation: Address before merge Overview
Issue Details (click to expand)WARNING
Files Reviewed (1 file)
Note: This PR adds a fail-safe mechanism ( |
There was a problem hiding this comment.
Code Review
This pull request addresses an issue where requests could hang indefinitely by introducing a maxWait timeout to the Bottleneck rate limiters. The change correctly applies this new timeout to default settings, custom limiters, and auto-enabled limiters. My review includes a suggestion to improve the parsing of the timeout value from environment variables for better robustness and code consistency.
| // Max time (ms) a job can wait in queue before failing with a timeout error. | ||
| // Prevents infinite queuing when all providers are exhausted after a 429. | ||
| // Configurable via RATE_LIMIT_MAX_WAIT_MS env var (default: 2 minutes). | ||
| const MAX_WAIT_MS = parseInt(process.env.RATE_LIMIT_MAX_WAIT_MS || "120000", 10); |
There was a problem hiding this comment.
The current parsing of RATE_LIMIT_MAX_WAIT_MS can result in NaN if a non-numeric string is provided in the environment variable, which could cause unexpected behavior in the Bottleneck limiter. For safer and more consistent parsing, it's better to use the existing toNumber helper function, which is designed to handle such cases gracefully.
| const MAX_WAIT_MS = parseInt(process.env.RATE_LIMIT_MAX_WAIT_MS || "120000", 10); | |
| const MAX_WAIT_MS = toNumber(process.env.RATE_LIMIT_MAX_WAIT_MS, 120000); |
There was a problem hiding this comment.
Pull request overview
Adds a configurable queue timeout to the adaptive rate limiting layer to prevent requests from waiting indefinitely when providers are rate-limited/exhausted (e.g., sustained 429 conditions).
Changes:
- Introduces
RATE_LIMIT_MAX_WAIT_MS(default 120000ms) to cap how long requests can remain queued. - Applies the timeout to both default limiter settings and limiters instantiated during startup initialization.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| // Max time (ms) a job can wait in queue before failing with a timeout error. | ||
| // Prevents infinite queuing when all providers are exhausted after a 429. | ||
| // Configurable via RATE_LIMIT_MAX_WAIT_MS env var (default: 2 minutes). | ||
| const MAX_WAIT_MS = parseInt(process.env.RATE_LIMIT_MAX_WAIT_MS || "120000", 10); |
There was a problem hiding this comment.
MAX_WAIT_MS is derived via parseInt(...) without validating the result. If RATE_LIMIT_MAX_WAIT_MS is set to a non-numeric value (or includes separators like "120_000"), parseInt can yield NaN/partial parses, which then propagates into Bottleneck options and breaks the documented default behavior. Please validate/clamp this env var (e.g., use the existing toNumber helper with a 120000 fallback and enforce >= 0).
| const MAX_WAIT_MS = parseInt(process.env.RATE_LIMIT_MAX_WAIT_MS || "120000", 10); | |
| const RAW_MAX_WAIT_MS = toNumber(process.env.RATE_LIMIT_MAX_WAIT_MS, 120000); | |
| const MAX_WAIT_MS = Math.max(0, Math.floor(RAW_MAX_WAIT_MS)); |
| reservoir: null, // No initial reservoir — unlimited until we learn | ||
| reservoirRefreshAmount: null, | ||
| reservoirRefreshInterval: null, | ||
| maxWait: MAX_WAIT_MS, // Fail-fast: don't queue forever on 429 exhaustion |
There was a problem hiding this comment.
CRITICAL: maxWait is not a valid Bottleneck option — Bottleneck v2.19.5 does not support this property. It will be silently ignored, and jobs will still queue indefinitely. The intended fail-fast behavior will not work.
Consider using maxQueue to limit queue size (by count), or implementing a custom timeout wrapper using Promise.race() with a timeout promise.
| reservoir: rpm, | ||
| reservoirRefreshAmount: rpm, | ||
| reservoirRefreshInterval: 60 * 1000, | ||
| maxWait: MAX_WAIT_MS, |
There was a problem hiding this comment.
CRITICAL: maxWait is not a valid Bottleneck option — this will be silently ignored. See comment on line 74.
| reservoir: DEFAULT_API_LIMITS.requestsPerMinute, | ||
| reservoirRefreshAmount: DEFAULT_API_LIMITS.requestsPerMinute, | ||
| reservoirRefreshInterval: 60 * 1000, // Refresh every minute | ||
| maxWait: MAX_WAIT_MS, |
There was a problem hiding this comment.
CRITICAL: maxWait is not a valid Bottleneck option — this will be silently ignored. See comment on line 74.
Code Review SummaryStatus: 4 Issues Found | Recommendation: Address before merge Overview
Issue Details (click to expand)CRITICAL
WARNING
Files Reviewed (1 file)
Fix Link: Fix these issues in Kilo Cloud |
…297-rate-limit-maxwait fix(rate-limit): prevent endless queue with maxWait (cheaper-inference#297)
fix(docker): healthcheck now uses /api/monitoring/health (cheaper-inference#296, PR cheaper-inference#301) fix(rate-limit): maxWait=120s on Bottleneck prevents endless queue (cheaper-inference#297, PR cheaper-inference#302)
…297-rate-limit-maxwait fix(rate-limit): prevent endless queue with maxWait (cheaper-inference#297)
fix(docker): healthcheck now uses /api/monitoring/health (cheaper-inference#296, PR cheaper-inference#301) fix(rate-limit): maxWait=120s on Bottleneck prevents endless queue (cheaper-inference#297, PR cheaper-inference#302)
Summary
Fixes #297 — requests hang indefinitely when all providers are rate-limited.
Root Cause
Bottleneck was configured without
maxWait, so when all providers returned 429 andreservoir=0, queued jobs waited forever. Clients (Cursor, Claude Code, VS Code) would show 'waiting...' indefinitely.Fix
Added
maxWait: MAX_WAIT_MS(default 2 minutes, configurable viaRATE_LIMIT_MAX_WAIT_MS) to:DEFAULT_SETTINGS(all ad-hoc limiters)When a job exceeds
maxWait, Bottleneck throws aBottleneckError, which propagates as a clean 502/503 error to the client instead of hanging.