Skip to content

Commit 538ff08

Browse files
remove undici interceptor, add fetchWithRetry
1 parent eaafcda commit 538ff08

2 files changed

Lines changed: 26 additions & 25 deletions

File tree

src/orkes/helpers/resolveFetchFn.ts

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,8 @@ export const resolveFetchFn = async (
1212
try {
1313
// eslint-disable-next-line
1414
// @ts-ignore since undici is an optional dependency and could me missing
15-
const { fetch: undiciFetch, Agent, interceptors } = await import("undici");
16-
const undiciAgent = new Agent({
17-
allowH2: true,
18-
//connections: 1,
19-
//keepAliveTimeout: 500,
20-
//clientTtl: 500,
21-
//keepAliveMaxTimeout: 1000 * 60 * 10,
22-
//pipelining: 1,
23-
connect: {
24-
rejectUnauthorized: true, // Ensure TLS validation
25-
secureProtocol: "TLSv1_2_method", // Use TLS 1.2
26-
},
27-
}).compose(
28-
interceptors.retry({
29-
maxRetries: 10, // Maximum number of retries (default: 5)
30-
minTimeout: 2000, // Minimum time to wait before retrying (default: 500ms)
31-
maxTimeout: 60000, // Maximum time to wait before retrying (default: 30000ms)
32-
timeoutFactor: 2, // Factor to multiply the timeout by for each retry (default: 2)
33-
//retryAfter: true, // Automatically retry if Retry-After header is present (default: true)
34-
methods: ["GET", "PUT", "HEAD", "POST", "DELETE", "PATCH", "OPTIONS"], // HTTP methods to retry (default includes GET, PUT, HEAD, OPTIONS, DELETE)
35-
//statusCodes: [429, 500, 502], // HTTP status codes to retry (default includes 429, 500, 502, 503, 504)
36-
errorCodes: ["ECONNRESET", "read ECONNRESET"], // Error codes to retry (default includes common network errors)
37-
})
38-
);
15+
const { fetch: undiciFetch, Agent } = await import("undici");
16+
const undiciAgent = new Agent({ allowH2: true });
3917

4018
return ((input: RequestInfo, init?: RequestInit) =>
4119
undiciFetch(input, { ...init, dispatcher: undiciAgent })) as FetchFn;

src/orkes/request/request.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,29 @@ const getRequestBody = (options: ApiRequestOptions): any => {
207207
return undefined;
208208
};
209209

210+
const fetchWithRetry = async (
211+
url: string,
212+
request: RequestInit,
213+
fetchFn: FetchFn<RequestInit, Response>,
214+
retries: number = 5,
215+
delay: number = 1000
216+
): Promise<Response> => {
217+
try {
218+
const response = await fetchFn(url, request);
219+
if (response.status == 429) {
220+
throw new Error(`Request failed with status ${response.status}`);
221+
}
222+
return response;
223+
} catch (error: unknown) {
224+
if (retries > 0) {
225+
console.warn(`Fetch error encountered. Retrying request in ${delay}ms...`);
226+
await new Promise(resolve => setTimeout(resolve, delay));
227+
return await fetchWithRetry(url, request, fetchFn, retries - 1, delay * 2);
228+
}
229+
throw error;
230+
}
231+
};
232+
210233
const sendRequest = async (
211234
options: ApiRequestOptions,
212235
url: string,
@@ -227,7 +250,7 @@ const sendRequest = async (
227250

228251
onCancel(() => controller.abort());
229252

230-
return await fetchFn(url, request);
253+
return await fetchWithRetry(url, request, fetchFn);
231254
};
232255

233256
const getResponseHeader = (

0 commit comments

Comments
 (0)