Viewing File: /home/ubuntu/efiexchange-node-base/src/controllers/exchange/functions/safeFetch.ts
import fetch from 'node-fetch';
import { HttpsAgent } from 'agentkeepalive';
const keepAliveAgent = new HttpsAgent({
keepAlive: true,
// maxSockets: 10,
// maxFreeSockets: 5,
timeout: 60000,
freeSocketTimeout: 30000,
});
export const safeFetch = async (url: string, options = {}, retries = 3, delay = 1000): Promise<any> => {
const opts = {
...options,
agent: keepAliveAgent,
};
for (let i = 0; i < retries; i++) {
try {
const res = await fetch(url, opts);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res.json();
} catch (err) {
if (i === retries - 1) throw err;
await new Promise(r => setTimeout(r, delay));
}
}
};
Back to Directory
File Manager