Lint retried fetch
This commit is contained in:
parent
b53cc5beb4
commit
e15b9e2498
1 changed files with 13 additions and 17 deletions
|
|
@ -3,33 +3,6 @@ import { Logger } from "src/tracing/logger";
|
||||||
|
|
||||||
const fetchWithRetry = fetchRetryFactory.default(fetch);
|
const fetchWithRetry = fetchRetryFactory.default(fetch);
|
||||||
|
|
||||||
export async function retriedFetch(
|
|
||||||
input: RequestInfo | URL,
|
|
||||||
init: RequestInit = {}
|
|
||||||
): Promise<Response> {
|
|
||||||
return fetchWithRetry(input, {
|
|
||||||
...init,
|
|
||||||
retryOn: function (attempt, error, response) {
|
|
||||||
// retry on any network error, or 4xx or 5xx status codes
|
|
||||||
if (error !== null || !response || response.status >= 500) {
|
|
||||||
Logger.getInstance().warn(
|
|
||||||
`Retrying fetch attempt ${attempt} for ${getUrlFromInput(
|
|
||||||
input
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
},
|
|
||||||
retries: 6,
|
|
||||||
retryDelay: function (attempt) {
|
|
||||||
Logger;
|
|
||||||
return Math.pow(1.5, attempt) * 500;
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function getUrlFromInput(input: RequestInfo | URL): string {
|
function getUrlFromInput(input: RequestInfo | URL): string {
|
||||||
if (input instanceof URL) {
|
if (input instanceof URL) {
|
||||||
return input.href;
|
return input.href;
|
||||||
|
|
@ -39,3 +12,26 @@ function getUrlFromInput(input: RequestInfo | URL): string {
|
||||||
}
|
}
|
||||||
return input.url;
|
return input.url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function retriedFetch(
|
||||||
|
input: RequestInfo | URL,
|
||||||
|
init: RequestInit = {}
|
||||||
|
): Promise<Response> {
|
||||||
|
return fetchWithRetry(input, {
|
||||||
|
...init,
|
||||||
|
retryOn: function (attempt, error, response) {
|
||||||
|
if (error !== null || !response || response.status >= 500) {
|
||||||
|
Logger.getInstance().warn(
|
||||||
|
`Retrying fetch for ${getUrlFromInput(
|
||||||
|
input
|
||||||
|
)}, attempt ${attempt}`
|
||||||
|
);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
retries: 6,
|
||||||
|
retryDelay: (attempt) => Math.pow(1.5, attempt) * 500,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue