All Projects → adobe → node-fetch-retry

adobe / node-fetch-retry

Licence: Apache-2.0 license
Node Module for performing retries using node-fetch

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to node-fetch-retry

ember-cli-es6-transform
Import ES6 modules from npm, bower or anywhere else in your app.
Stars: ✭ 13 (-60.61%)
Mutual labels:  node-module
node-mysql
Node with mysql boilerplate
Stars: ✭ 72 (+118.18%)
Mutual labels:  node-module
node-typescript-starter
REST API using Node with typescript, KOA framework. TypeORM for SQL. Middlewares JWT (auth), CORS, Winston Logger, Error, Response
Stars: ✭ 19 (-42.42%)
Mutual labels:  node-module
originator
🌱 es6 starter - babel, tape, zuul, npm scripts
Stars: ✭ 12 (-63.64%)
Mutual labels:  node-module
cache-all
Fast, efficient cache engines for both express routes & native nodeJs (redis, in-memory & file cache)
Stars: ✭ 22 (-33.33%)
Mutual labels:  node-module
ugql
🚀GraphQL.js over HTTP with uWebSockets.js
Stars: ✭ 27 (-18.18%)
Mutual labels:  node-module
lambda-mailer
Simple module for receiving an email from a contact form on your website.
Stars: ✭ 83 (+151.52%)
Mutual labels:  node-module
node-health-check
Build health check functions which comply with the FT health check standard
Stars: ✭ 20 (-39.39%)
Mutual labels:  node-module
recastCLI.js
CLI tool & Node.js addon to generate navigation mesh
Stars: ✭ 36 (+9.09%)
Mutual labels:  node-module
djb2a
DJB2a non-cryptographic hash function
Stars: ✭ 31 (-6.06%)
Mutual labels:  node-module
ioBroker.epson stylus px830
Zustand Druckerpatronen im EPSON Stylus PX830 für ioBroker auslesen
Stars: ✭ 18 (-45.45%)
Mutual labels:  node-module
xlsx-extract
nodejs lib for extracting data from XLSX files
Stars: ✭ 33 (+0%)
Mutual labels:  node-module
origami-build-tools
Standard Origami component development tools.
Stars: ✭ 49 (+48.48%)
Mutual labels:  node-module
the-traveler
The Traveler is a small npm package which wraps around the Destiny 2 API.
Stars: ✭ 52 (+57.58%)
Mutual labels:  node-module
node-screenlogic
Pentair ScreenLogic Javascript library using Node.JS
Stars: ✭ 38 (+15.15%)
Mutual labels:  node-module
node-cli-boilerplate
🪓 Create node cli with this user friendly boilerplate
Stars: ✭ 17 (-48.48%)
Mutual labels:  node-module
vayder
Easy and concise validations for Express routes
Stars: ✭ 26 (-21.21%)
Mutual labels:  node-module
sdbm
SDBM non-cryptographic hash function
Stars: ✭ 43 (+30.3%)
Mutual labels:  node-module
node-movehub
Node.js interface for the Lego Boost Move Hub 🤖 🐱 🎸 🚚
Stars: ✭ 57 (+72.73%)
Mutual labels:  node-module
crc16
A native node addon to calcalate and verify CRC16 values, adopted by MODBUS agreement
Stars: ✭ 24 (-27.27%)
Mutual labels:  node-module

Version License Travis

node-fetch-retry

Node Module for performing retries for HTTP requests.

It is a wrapper around node-fetch library. It has default retry logic built in as described below, as well as configurable parameters. It also has built-in support for Apache OpenWhisk actions, adjusting the timeout to reflect the action timeout.

Installation

npm install @adobe/node-fetch-retry

Usage

This library works the same as the normal fetch api, but with some added features.

Default Behavior

Without configuring any parameters, the retry behavior will be as follows:

const fetch = require('@adobe/node-fetch-retry');

async main() {
    const response = await fetch(url);
}

This example uses only custom headers and will use default retry settings:

const fetch = require('@adobe/node-fetch-retry');

async main() {
    const response = await fetch(url, {
        headers: {
            'custom-header': '<<put custom header value here>>'
        }
    });
}

Optional Custom Parameters

All the retry options are configurable and can be set in retryOptions in the options object passed to fetch.

Parameter Format Description Environment variable Default Value
retryMaxDuration Number time in milliseconds to retry until throwing an error NODE_FETCH_RETRY_MAX_RETRY 60000 ms
retryInitialDelay Number time in milliseconds to wait between retries NODE_FETCH_RETRY_INITIAL_WAIT 100 ms
retryBackoff Number backoff factor for wait time between retries NODE_FETCH_RETRY_BACKOFF 2.0
retryOnHttpResponse Function a function determining whether to retry given the HTTP response. Can be asynchronous none retry on all 5xx errors
retryOnHttpError Function a function determining whether to retry given the HTTP error exception thrown. Can be asynchronous none retry on all FetchError's of type system
socketTimeout Number time until socket timeout in milliseconds. Note: if socketTimeout is >= retryMaxDuration, it will automatically adjust the socket timeout to be exactly half of the retryMaxDuration. To disable this feature, see forceSocketTimeout below NODE_FETCH_RETRY_SOCKET_TIMEOUT 30000 ms
forceSocketTimeout Boolean If true, socket timeout will be forced to use socketTimeout property declared regardless of the retryMaxDuration. Note: this feature was designed to help with unit testing and is not intended to be used in practice NODE_FETCH_RETRY_FORCE_TIMEOUT false

Note: the environment variables override the default values if the corresponding parameter is not set. These are designed to help with unit testing. Passed in parameters will still override the environment variables

Custom Parameter Examples

This example decreases the retryMaxDuration and makes the retry delay a static 500ms. This will do no more than 4 retries.

const fetch = require('@adobe/node-fetch-retry');

async main() {
    const response = await fetch(url, {
        retryOptions: {
            retryMaxDuration: 2000,  // 30s retry max duration
            retryInitialDelay: 500,
            retryBackoff: 1.0 // no backoff
        }
    });
}

This example shows how to configure retries on specific HTTP responses:

const fetch = require('@adobe/node-fetch-retry');

async main() {
    const response = await fetch(url, {
        retryOptions: {
            retryOnHttpResponse: function (response) {
                if ( (response.status >= 500) || response.status >= 400) { // retry on all 5xx and all 4xx errors
                    return true;
                }
            }
        }
    });
}

This example uses custom socketTimeout values:

const fetch = require('@adobe/node-fetch-retry');

async main() {
    const response = await fetch(url, {
        retryOptions: {
            retryMaxDuration: 300000, // 5min retry duration
            socketTimeout: 60000, //  60s socket timeout
        }
    });
}

This example uses custom socketTimeout values and custom headers:

const fetch = require('@adobe/node-fetch-retry');

async main() {
    const response = await fetch(url, {
        retryOptions: {
            retryMaxDuration: 300000, // 5min retry duration
            socketTimeout: 60000, //  60s socket timeout
        },
        headers: {
            'custom-header': '<<put custom header value here>>'
        }
    });
}

This example shows how to retry on all HTTP errors thrown as an exception:

const fetch = require('@adobe/node-fetch-retry');

async main() {
    const response = await fetch(url, {
        retryOptions: {
            retryOnHttpError: function (error) {
                return true;
            }
        }
    });
}

Disable Retry

You can disable all retry behavior by setting retryOptions to false.

const fetch = require('@adobe/node-fetch-retry');

async main() {
    const response = await fetch(url, {
        retryOptions: false
    });
}

Disabling retry behavior will not prevent the usage of other options set on the options object.

Additional notes on retry duration

If the fetch is unsuccessful, the retry logic determines how long it will wait before the next attempt. If the time remaining will exceed the total time allowed by retryMaxDuration then another attempt will not be made. There are examples of how this works in the testing code.

Apache OpenWhisk Action Support

If you are running this in the context of an OpenWhisk action, it will take into account the action timeout deadline when setting the retryMaxDuration. It uses the __OW_ACTION_DEADLINE environment variable to determine if there is an action running.

Behavior: If retryMaxDuration is greater than the time till the action will timeout, it will adjust the retryMaxDuration to be equal to the time till action timeout.

Contributing

Contributions are welcomed! Read the Contributing Guide for more information.

Licensing

This project is licensed under the Apache V2 License. See LICENSE for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].