mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 13:38:31 +00:00
feat(base): add 429 retry with fixed delay for all providers
Retry up to 2 times with 2s delay before falling back to next URL. Made-with: Cursor
This commit is contained in:
committed by
decolua
parent
6437a1c55f
commit
36f8a8ce16
@@ -461,6 +461,12 @@ export const DEFAULT_MAX_TOKENS = 64000;
|
|||||||
// Minimum max tokens for tool calling (to prevent truncated arguments)
|
// Minimum max tokens for tool calling (to prevent truncated arguments)
|
||||||
export const DEFAULT_MIN_TOKENS = 32000;
|
export const DEFAULT_MIN_TOKENS = 32000;
|
||||||
|
|
||||||
|
// Retry config for 429 responses (used by BaseExecutor)
|
||||||
|
export const RETRY_CONFIG = {
|
||||||
|
maxAttempts: 2,
|
||||||
|
delayMs: 2000
|
||||||
|
};
|
||||||
|
|
||||||
// HTTP status codes
|
// HTTP status codes
|
||||||
export const HTTP_STATUS = {
|
export const HTTP_STATUS = {
|
||||||
BAD_REQUEST: 400,
|
BAD_REQUEST: 400,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { HTTP_STATUS } from "../config/constants.js";
|
import { HTTP_STATUS, RETRY_CONFIG } from "../config/constants.js";
|
||||||
import { proxyAwareFetch } from "../utils/proxyFetch.js";
|
import { proxyAwareFetch } from "../utils/proxyFetch.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -80,12 +80,15 @@ export class BaseExecutor {
|
|||||||
const fallbackCount = this.getFallbackCount();
|
const fallbackCount = this.getFallbackCount();
|
||||||
let lastError = null;
|
let lastError = null;
|
||||||
let lastStatus = 0;
|
let lastStatus = 0;
|
||||||
|
const retryAttemptsByUrl = {};
|
||||||
|
|
||||||
for (let urlIndex = 0; urlIndex < fallbackCount; urlIndex++) {
|
for (let urlIndex = 0; urlIndex < fallbackCount; urlIndex++) {
|
||||||
const url = this.buildUrl(model, stream, urlIndex, credentials);
|
const url = this.buildUrl(model, stream, urlIndex, credentials);
|
||||||
const headers = this.buildHeaders(credentials, stream);
|
const headers = this.buildHeaders(credentials, stream);
|
||||||
const transformedBody = this.transformRequest(model, body, stream, credentials);
|
const transformedBody = this.transformRequest(model, body, stream, credentials);
|
||||||
|
|
||||||
|
if (!retryAttemptsByUrl[urlIndex]) retryAttemptsByUrl[urlIndex] = 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await proxyAwareFetch(url, {
|
const response = await proxyAwareFetch(url, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@@ -94,6 +97,15 @@ export class BaseExecutor {
|
|||||||
signal
|
signal
|
||||||
}, proxyOptions);
|
}, proxyOptions);
|
||||||
|
|
||||||
|
// Retry 429 with fixed delay before falling back to next URL
|
||||||
|
if (response.status === HTTP_STATUS.RATE_LIMITED && retryAttemptsByUrl[urlIndex] < RETRY_CONFIG.maxAttempts) {
|
||||||
|
retryAttemptsByUrl[urlIndex]++;
|
||||||
|
log?.debug?.("RETRY", `429 retry ${retryAttemptsByUrl[urlIndex]}/${RETRY_CONFIG.maxAttempts} after ${RETRY_CONFIG.delayMs / 1000}s`);
|
||||||
|
await new Promise(resolve => setTimeout(resolve, RETRY_CONFIG.delayMs));
|
||||||
|
urlIndex--;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (this.shouldRetry(response.status, urlIndex)) {
|
if (this.shouldRetry(response.status, urlIndex)) {
|
||||||
log?.debug?.("RETRY", `${response.status} on ${url}, trying fallback ${urlIndex + 1}`);
|
log?.debug?.("RETRY", `${response.status} on ${url}, trying fallback ${urlIndex + 1}`);
|
||||||
lastStatus = response.status;
|
lastStatus = response.status;
|
||||||
|
|||||||
Reference in New Issue
Block a user