Inline fetch-retry with cancellation
This commit is contained in:
parent
d772cda164
commit
a9223156a6
7 changed files with 109 additions and 100 deletions
|
|
@ -1,38 +0,0 @@
|
|||
import * as fetchRetryFactory from "fetch-retry";
|
||||
import type { RequestInitRetryParams } from "fetch-retry";
|
||||
import type { Logger } from "../tracing/logger";
|
||||
|
||||
function getUrlFromInput(input: RequestInfo | URL): string {
|
||||
if (input instanceof URL) {
|
||||
return input.href;
|
||||
}
|
||||
if (typeof input === "string") {
|
||||
return input;
|
||||
}
|
||||
return input.url;
|
||||
}
|
||||
|
||||
export function retriedFetchFactory(
|
||||
logger: Logger,
|
||||
fetch: typeof globalThis.fetch = globalThis.fetch
|
||||
) {
|
||||
return async (
|
||||
input: RequestInfo | URL,
|
||||
init: RequestInitRetryParams<typeof fetch> = {}
|
||||
): Promise<Response> => {
|
||||
return fetchRetryFactory.default(fetch)(input, {
|
||||
retryOn: function (attempt, error, response) {
|
||||
if (error !== null || !response || response.status >= 500) {
|
||||
logger.warn(
|
||||
`Retrying fetch for ${getUrlFromInput(input)}, attempt ${attempt}`
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
retryDelay: (attempt) => Math.pow(1.5, attempt) * 500,
|
||||
...init
|
||||
});
|
||||
};
|
||||
}
|
||||
3
frontend/sync-client/src/utils/sleep.ts
Normal file
3
frontend/sync-client/src/utils/sleep.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export async function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue