Skip to content

Commit

Permalink
enhance(server): retry rari fetch if ECONNREFUSED
Browse files Browse the repository at this point in the history
  • Loading branch information
caugner committed Jan 24, 2025
1 parent 21ba850 commit 7846bbd
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,45 @@ import { handleRunner } from "../libs/play/index.js";
async function fetch_from_rari(path: string) {
const external_url = `${EXTERNAL_DEV_SERVER}${path}`;
console.log(`using ${external_url}`);
// eslint-disable-next-line n/no-unsupported-features/node-builtins
return await (await fetch(external_url)).json();
const response = await fetchRetryIfRefused(external_url, 5);
return await response.json();
}

async function fetchRetryIfRefused(
url: string,
maxRetries: number,
baseDelay = 1000
) {
let attempt = 0;

while (attempt < maxRetries) {
try {
// eslint-disable-next-line n/no-unsupported-features/node-builtins
const response = await fetch(url);
if (!response.ok) {
throw new Error(`failed with HTTP ${response.status}`);
}
return response;
} catch (error: any) {
attempt++;
if (
error.code !== "ECONNREFUSED" &&
error.cause?.code !== "ECONNREFUSED"
) {
throw error;
}

console.log(`retrying ${attempt}/${maxRetries}`);
if (attempt < maxRetries) {
const delay = baseDelay * Math.pow(2, attempt - 1); // Exponential backoff
await new Promise((resolve) => setTimeout(resolve, delay));
} else {
throw new Error(
`failed after ${maxRetries} attempts: ${error.message}`
);
}
}
}
}

async function buildDocumentFromURL(url: string) {
Expand Down

0 comments on commit 7846bbd

Please sign in to comment.