mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-13 21:32:56 -07:00
refactor(core): extract backoff delay helper in retry.ts
Extracts the duplicated delay and backoff logic in retryWithBackoff into a helper function applyBackoffDelay to improve maintainability. TAG=agy CONV=37b3cdb6-8437-4ced-ae4e-3d04054bacb1
This commit is contained in:
@@ -307,8 +307,7 @@ export async function retryWithBackoff<T>(
|
||||
) {
|
||||
if (onPersistent429) {
|
||||
try {
|
||||
const currentContext = getAvailabilityContext?.();
|
||||
const currentModel = currentContext?.policy.model;
|
||||
const currentModel = getAvailabilityContext?.()?.policy.model;
|
||||
|
||||
const fallbackModel = await onPersistent429(
|
||||
authType,
|
||||
@@ -326,10 +325,11 @@ export async function retryWithBackoff<T>(
|
||||
continue;
|
||||
} else {
|
||||
// If it's the same model (or a boolean retry signal), wait before retrying
|
||||
const jitter = currentDelay * 0.3 * (Math.random() * 2 - 1);
|
||||
const delayWithJitter = Math.max(0, currentDelay + jitter);
|
||||
await delay(delayWithJitter, signal);
|
||||
currentDelay = Math.min(maxDelayMs, currentDelay * 2);
|
||||
currentDelay = await applyBackoffDelay(
|
||||
currentDelay,
|
||||
maxDelayMs,
|
||||
signal,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -373,8 +373,7 @@ export async function retryWithBackoff<T>(
|
||||
);
|
||||
if (onPersistent429) {
|
||||
try {
|
||||
const currentContext = getAvailabilityContext?.();
|
||||
const currentModel = currentContext?.policy.model;
|
||||
const currentModel = getAvailabilityContext?.()?.policy.model;
|
||||
|
||||
const fallbackModel = await onPersistent429(
|
||||
authType,
|
||||
@@ -392,10 +391,11 @@ export async function retryWithBackoff<T>(
|
||||
continue;
|
||||
} else {
|
||||
// If it's the same model (or a boolean retry signal), wait before retrying
|
||||
const jitter = currentDelay * 0.3 * (Math.random() * 2 - 1);
|
||||
const delayWithJitter = Math.max(0, currentDelay + jitter);
|
||||
await delay(delayWithJitter, signal);
|
||||
currentDelay = Math.min(maxDelayMs, currentDelay * 2);
|
||||
currentDelay = await applyBackoffDelay(
|
||||
currentDelay,
|
||||
maxDelayMs,
|
||||
signal,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -506,3 +506,14 @@ function logRetryAttempt(
|
||||
debugLogger.warn(message, error); // Default to warn if error type is unknown
|
||||
}
|
||||
}
|
||||
|
||||
async function applyBackoffDelay(
|
||||
currentDelay: number,
|
||||
maxDelayMs: number,
|
||||
signal?: AbortSignal,
|
||||
): Promise<number> {
|
||||
const jitter = currentDelay * 0.3 * (Math.random() * 2 - 1);
|
||||
const delayWithJitter = Math.max(0, currentDelay + jitter);
|
||||
await delay(delayWithJitter, signal);
|
||||
return Math.min(maxDelayMs, currentDelay * 2);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user