Skip to content

BREAKING-RELEASE: v3 [work in progress] #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ Without configuring any parameters, the retry behavior will be as follows:
- retry for 60s
- retry inital delay of 100ms with exponential backoff, configurable as a multiplier defaulting to 2
- retry only on 5xx response
- retry on all FetchError system errors
- retry on all errors thrown by fetch
- see node-fetch error handling: https://github.com/node-fetch/node-fetch/blob/main/docs/ERROR-HANDLING.md
- with special behavior to avoid retrying on developer errors (program errors)
- this includes `AbortError`'s, `FetchError`'s
- socket timeout of 30s
```js
const fetch = require('@adobe/node-fetch-retry');
Expand Down
106 changes: 42 additions & 64 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
const AbortController = require('abort-controller');
const fetch = require('node-fetch');
const {FetchError} = fetch;
const sleep = require('util').promisify(setTimeout);

function getTimeRemaining(retryOptions) {
if (retryOptions && retryOptions.startTime && retryOptions.retryMaxDuration) {
Expand Down Expand Up @@ -99,7 +100,7 @@ function retryInit(options={}) {
retryOnHttpResponse: ((typeof retryOptions.retryOnHttpResponse === 'function') && retryOptions.retryOnHttpResponse) ||
((response) => { return response.status >= 500; }),
retryOnHttpError: ((typeof retryOptions.retryOnHttpError === 'function') && retryOptions.retryOnHttpError) ||
((error) => { return shouldRetryOnHttpError(error); }),
(() => { return true; }),
socketTimeout: socketTimeoutValue
};
}
Expand Down Expand Up @@ -145,25 +146,6 @@ function checkParameters(retryOptions) {
}
}

/**
* Evaluates whether or not to retry based on HTTP error
* @param {Object} error
* @returns Returns true for all FetchError's of type `system`
*/
function shouldRetryOnHttpError(error) {
// special handling for known fetch errors: https://github.com/node-fetch/node-fetch/blob/main/docs/ERROR-HANDLING.md
// retry on all errors originating from Node.js core
// retry on AbortError caused by network timeouts
if (error.name === 'FetchError' && error.type === 'system') {
console.error(`FetchError failed with code: ${error.code}; message: ${error.message}`);
return true;
} else if (error.name === 'AbortError') {
console.error(`AbortError failed with type: ${error.type}; message: ${error.message}`);
return true;
}
return false;
}

/**
* @typedef {Object} RetryOptions options for retry or false if want to disable retry
* @property {Integer} retryMaxDuration time (in milliseconds) to retry until throwing an error
Expand Down Expand Up @@ -196,54 +178,50 @@ function shouldRetryOnHttpError(error) {
* @returns {Object} json response of calling fetch
*/
module.exports = async function (url, options) {
options = options || {};
options = {...options} || {};
const retryOptions = retryInit(options); // set up retry options or set to default settings if not set
delete options.retryOptions; // remove retry options from options passed to actual fetch
let attempt = 0;

return new Promise(function (resolve, reject) {
const wrappedFetch = async () => {
while (!isResponseTimedOut(retryOptions)) {
++attempt;
const waitTime = getRetryDelay(retryOptions);

let timeoutHandler;
if (retryOptions.socketTimeout) {
const controller = new AbortController();
timeoutHandler = setTimeout(() => controller.abort(), retryOptions.socketTimeout);
options.signal = controller.signal;
}

try {
const response = await fetch(url, options);

if (await shouldRetry(retryOptions, null, response, waitTime)) {
console.error(`Retrying in ${waitTime} milliseconds, attempt ${attempt} failed (status ${response.status}): ${response.statusText}`);
} else {
// response.timeout should reflect the actual timeout
response.timeout = retryOptions.socketTimeout;
return resolve(response);
}
} catch (error) {
if (!(await shouldRetry(retryOptions, error, null, waitTime))) {
if (error.name === 'AbortError') {
return reject(new FetchError(`network timeout at ${url}`, 'request-timeout'));
} else {
return reject(error);
}
}
console.error(`Retrying in ${waitTime} milliseconds, attempt ${attempt} error: ${error.name}, ${error.message}`);
} finally {
clearTimeout(timeoutHandler);
}
// Fetch loop is about to repeat, delay as needed first.
if (waitTime > 0) {
await new Promise(resolve => setTimeout(resolve, waitTime));
while (!isResponseTimedOut(retryOptions)) {
++attempt;
const waitTime = getRetryDelay(retryOptions);

let timeoutHandler;
if (retryOptions.socketTimeout) {
const controller = new AbortController();
timeoutHandler = setTimeout(() => controller.abort(), retryOptions.socketTimeout);
options.signal = controller.signal;
}

try {
const response = await fetch(url, options);

if (await shouldRetry(retryOptions, null, response, waitTime)) {
console.error(`Retrying in ${waitTime} milliseconds, attempt ${attempt} failed (status ${response.status}): ${response.statusText}`);
} else {
// response.timeout should reflect the actual timeout
response.timeout = retryOptions.socketTimeout;
return response;
}
} catch (error) {
if (!(await shouldRetry(retryOptions, error, null, waitTime))) {
if (error.name === 'AbortError') {
throw new FetchError(`network timeout at ${url}`, 'request-timeout');
} else {
throw error;
}
retryOptions.retryInitialDelay *= retryOptions.retryBackoff; // update retry interval
}
reject(new FetchError(`network timeout at ${url}`, 'request-timeout'));
};
wrappedFetch();
});
console.error(`Retrying in ${waitTime} milliseconds, attempt ${attempt} error: ${error.name}, ${error.message}`);
} finally {
clearTimeout(timeoutHandler);
}
// Fetch loop is about to repeat, delay as needed first.
if (waitTime > 0) {
await sleep(waitTime);
}
retryOptions.retryInitialDelay *= retryOptions.retryBackoff; // update retry interval
}

throw new FetchError(`network timeout at ${url}`, 'request-timeout');
};
Loading