mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 13:38:31 +00:00
fix: giảm spam 429 từ Claude OAuth usage endpoint
- claudeAutoPing: cache resetAt in-mem, bỏ qua poll usage cho tới gần reset - ProviderLimits: throttle auto-refresh Claude 3 phút, nút bấm tay vẫn refresh ngay - claude.js: 429 ở OAuth usage → cooldown 3 phút, fallback legacy (không ảnh hưởng chat) Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -14,11 +14,19 @@ const CLAUDE_CONFIG = {
|
||||
apiVersion: ANTHROPIC_API_VERSION,
|
||||
};
|
||||
|
||||
/**
|
||||
* Claude Usage - Primary: OAuth endpoint, Fallback: legacy settings/org endpoint
|
||||
*/
|
||||
// OAuth usage endpoint rate-limits (429); cool down per-token to stop hammering it.
|
||||
// Only the quota endpoint is affected — chat with the same token still works.
|
||||
const OAUTH_429_COOLDOWN_MS = 180000;
|
||||
const oauthCooldown = new Map();
|
||||
|
||||
export async function getClaudeUsage(accessToken, proxyOptions = null) {
|
||||
try {
|
||||
// Skip OAuth usage call while this token is cooling down from a recent 429
|
||||
const cooldownUntil = oauthCooldown.get(accessToken);
|
||||
if (cooldownUntil && Date.now() < cooldownUntil) {
|
||||
return await getClaudeUsageLegacy(accessToken, proxyOptions);
|
||||
}
|
||||
|
||||
// Primary: OAuth usage endpoint (Claude Code consumer OAuth tokens)
|
||||
const oauthResponse = await proxyAwareFetch(CLAUDE_CONFIG.oauthUsageUrl, {
|
||||
method: "GET",
|
||||
@@ -73,6 +81,11 @@ export async function getClaudeUsage(accessToken, proxyOptions = null) {
|
||||
};
|
||||
}
|
||||
|
||||
// Cool down OAuth usage polling after a 429 (quota endpoint only)
|
||||
if (oauthResponse.status === 429) {
|
||||
oauthCooldown.set(accessToken, Date.now() + OAUTH_429_COOLDOWN_MS);
|
||||
}
|
||||
|
||||
// Fallback: legacy settings + org usage endpoint
|
||||
console.warn(`[Claude Usage] OAuth endpoint returned ${oauthResponse.status}, falling back to legacy`);
|
||||
return await getClaudeUsageLegacy(accessToken, proxyOptions);
|
||||
|
||||
Reference in New Issue
Block a user