Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
/**
* rateLimitManager/admission — queue-depth admission check (pure).
*
* `maxQueueDepth` (RequestQueueSettings, issue #6593) is an opt-in admission
* cap on the local rate-limit queue: when set (>0), a request that would be
* queued behind `maxQueueDepth` already-queued jobs is fast-rejected before
* it ever reaches Bottleneck's `schedule()`, instead of growing the queue
* unboundedly. Default `0` = disabled, preserving today's behavior exactly.
*
* Extracted as a pure function (no Bottleneck/limiter dependency) so it is
* unit-testable without spinning up a real limiter.
*
* @module services/rateLimitManager/admission
*/
import { markLocalRateLimitError, RATE_LIMIT_QUEUE_FULL_CODE } from "./errors";
export interface QueueFullError extends Error {
code: typeof RATE_LIMIT_QUEUE_FULL_CODE;
status: 429;
}
/**
* Returns a typed `RATE_LIMIT_QUEUE_FULL` error when `queuedCount` is at or
* above `maxQueueDepth`, or `null` when admission should proceed (cap
* disabled, i.e. `maxQueueDepth <= 0`, or the queue has room).
*/
export function checkQueueAdmission(
queuedCount: number,
maxQueueDepth: number,
identity: string
): QueueFullError | null {
if (!maxQueueDepth || maxQueueDepth <= 0) return null;
if (queuedCount < maxQueueDepth) return null;
const err = new Error(
`Request rejected: the local rate-limit queue for ${identity} already holds ${queuedCount} ` +
`queued request(s), at or above the configured admission cap maxQueueDepth (${maxQueueDepth}) ` +
`— this is OmniRoute's request queue (resilienceSettings.requestQueue.maxQueueDepth), not an ` +
`upstream rejection. Raise it in Settings → Resilience if this is expected burst traffic.`
);
// The public code/status remain useful to callers, while the WeakMap brand
// is the provenance signal used by health and routing decisions.
return markLocalRateLimitError(err, RATE_LIMIT_QUEUE_FULL_CODE) as QueueFullError;
}