Retry failed review requests
This commit is contained in:
1 parent
50d0ae5be9
commit
fd9479590a
5 files changed
+191
-118
No files matched your search
@@ -0,0 +1,47 @@
|
||||
import { logger } from "./logger.js";
|
||||
|
||||
const MAX_ATTEMPTS = 5;
|
||||
const BASE_DELAY_MS = 1_000;
|
||||
|
||||
export async function withRetry<T>(
|
||||
label: string,
|
||||
operation: () => Promise<T>,
|
||||
options?: {
|
||||
maxAttempts?: number;
|
||||
baseDelayMs?: number;
|
||||
}
|
||||
): Promise<T> {
|
||||
const maxAttempts = options?.maxAttempts ?? MAX_ATTEMPTS;
|
||||
const baseDelayMs = options?.baseDelayMs ?? BASE_DELAY_MS;
|
||||
let lastError: unknown;
|
||||
|
||||
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
|
||||
try {
|
||||
return await operation();
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
|
||||
logger.warn(
|
||||
{
|
||||
error,
|
||||
label,
|
||||
attempt,
|
||||
maxAttempts
|
||||
},
|
||||
"Request failed."
|
||||
);
|
||||
|
||||
if (attempt === maxAttempts) {
|
||||
break;
|
||||
}
|
||||
|
||||
await delay(baseDelayMs * attempt);
|
||||
}
|
||||
}
|
||||
|
||||
throw lastError;
|
||||
}
|
||||
|
||||
function delay(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
Reference in new issue
Block a user