1 | [![Version](https://img.shields.io/npm/v/@adobe/node-fetch-retry.svg)](https://npmjs.org/package/@adobe/node-fetch-retry)
|
2 | [![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0)
|
3 | [![Travis](https://travis-ci.com/adobe/node-fetch-retry.svg?branch=master)](https://travis-ci.com/adobe/node-fetch-retry)
|
4 |
|
5 | # node-fetch-retry
|
6 |
|
7 | Node Module for performing retries for HTTP requests.
|
8 |
|
9 | It is a wrapper around [`node-fetch`](https://github.com/node-fetch/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.
|
10 |
|
11 | ## Installation
|
12 |
|
13 | ```bash
|
14 | npm install @adobe/node-fetch-retry
|
15 | ```
|
16 |
|
17 | ## Usage
|
18 |
|
19 | This library works the same as the normal [`fetch api`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API), but with some added features.
|
20 |
|
21 | ### Default Behavior
|
22 |
|
23 | Without configuring any parameters, the retry behavior will be as follows:
|
24 | - retry for 60s
|
25 | - retry inital delay of 100ms with exponential backoff, configurable as a multiplier defaulting to 2
|
26 | - retry only on 5xx response
|
27 | - retry on all FetchError system errors
|
28 | - see node-fetch error handling: https://github.com/node-fetch/node-fetch/blob/main/docs/ERROR-HANDLING.md
|
29 | - socket timeout of 30s
|
30 | ```js
|
31 | const fetch = require('@adobe/node-fetch-retry');
|
32 |
|
33 | async main() {
|
34 | const response = await fetch(url);
|
35 | }
|
36 | ```
|
37 |
|
38 | This example uses only custom headers and will use default retry settings:
|
39 |
|
40 | ```js
|
41 | const fetch = require('@adobe/node-fetch-retry');
|
42 |
|
43 | async main() {
|
44 | const response = await fetch(url, {
|
45 | headers: {
|
46 | 'custom-header': '<<put custom header value here>>'
|
47 | }
|
48 | });
|
49 | }
|
50 | ```
|
51 |
|
52 | ### Optional Custom Parameters
|
53 |
|
54 | All the retry options are configurable and can be set in `retryOptions` in the `options` object passed to `fetch`.
|
55 |
|
56 | | Parameter | Format | Description | Environment variable | Default Value |
|
57 | | --------- | ------ | ----------- | -------------------- | ------------- |
|
58 | | `retryMaxDuration` | Number | time in milliseconds to retry until throwing an error | `NODE_FETCH_RETRY_MAX_RETRY` | 60000 ms |
|
59 | | `retryInitialDelay` | Number | time in milliseconds to wait between retries |`NODE_FETCH_RETRY_INITIAL_WAIT` | 100 ms |
|
60 | | `retryBackoff` | Number | backoff factor for wait time between retries | `NODE_FETCH_RETRY_BACKOFF` | 2.0 |
|
61 | | `retryOnHttpResponse` | Function | a *function* determining whether to retry given the HTTP response. Can be asynchronous | none | retry on all 5xx errors|
|
62 | | `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`|
|
63 | | `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 |
|
64 | | `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 |
|
65 |
|
66 | _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_
|
67 |
|
68 | ### Custom Parameter Examples
|
69 |
|
70 | This example decreases the `retryMaxDuration` and makes the retry delay a static 500ms. This will do no more than 4 retries.
|
71 | ```js
|
72 | const fetch = require('@adobe/node-fetch-retry');
|
73 |
|
74 | async main() {
|
75 | const response = await fetch(url, {
|
76 | retryOptions: {
|
77 | retryMaxDuration: 2000, // 30s retry max duration
|
78 | retryInitialDelay: 500,
|
79 | retryBackoff: 1.0 // no backoff
|
80 | }
|
81 | });
|
82 | }
|
83 | ```
|
84 |
|
85 | This example shows how to configure retries on specific HTTP responses:
|
86 |
|
87 | ```js
|
88 | const fetch = require('@adobe/node-fetch-retry');
|
89 |
|
90 | async main() {
|
91 | const response = await fetch(url, {
|
92 | retryOptions: {
|
93 | retryOnHttpResponse: function (response) {
|
94 | if ( (response.status >= 500) || response.status >= 400) { // retry on all 5xx and all 4xx errors
|
95 | return true;
|
96 | }
|
97 | }
|
98 | }
|
99 | });
|
100 | }
|
101 | ```
|
102 |
|
103 | This example uses custom `socketTimeout` values:
|
104 |
|
105 | ```js
|
106 | const fetch = require('@adobe/node-fetch-retry');
|
107 |
|
108 | async main() {
|
109 | const response = await fetch(url, {
|
110 | retryOptions: {
|
111 | retryMaxDuration: 300000, // 5min retry duration
|
112 | socketTimeout: 60000, // 60s socket timeout
|
113 | }
|
114 | });
|
115 | }
|
116 | ```
|
117 |
|
118 | This example uses custom `socketTimeout` values and custom headers:
|
119 |
|
120 | ```js
|
121 | const fetch = require('@adobe/node-fetch-retry');
|
122 |
|
123 | async main() {
|
124 | const response = await fetch(url, {
|
125 | retryOptions: {
|
126 | retryMaxDuration: 300000, // 5min retry duration
|
127 | socketTimeout: 60000, // 60s socket timeout
|
128 | },
|
129 | headers: {
|
130 | 'custom-header': '<<put custom header value here>>'
|
131 | }
|
132 | });
|
133 | }
|
134 | ```
|
135 | This example shows how to retry on all HTTP errors thrown as an exception:
|
136 | ```js
|
137 | const fetch = require('@adobe/node-fetch-retry');
|
138 |
|
139 | async main() {
|
140 | const response = await fetch(url, {
|
141 | retryOptions: {
|
142 | retryOnHttpError: function (error) {
|
143 | return true;
|
144 | }
|
145 | }
|
146 | });
|
147 | }
|
148 | ```
|
149 |
|
150 |
|
151 | ### Disable Retry
|
152 |
|
153 | You can disable all retry behavior by setting `retryOptions` to `false`.
|
154 |
|
155 | ```js
|
156 | const fetch = require('@adobe/node-fetch-retry');
|
157 |
|
158 | async main() {
|
159 | const response = await fetch(url, {
|
160 | retryOptions: false
|
161 | });
|
162 | }
|
163 | ```
|
164 |
|
165 | Disabling retry behavior will not prevent the usage of other options set on the `options` object.
|
166 |
|
167 | ### Additional notes on retry duration
|
168 |
|
169 | 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.
|
170 |
|
171 | ### Apache OpenWhisk Action Support
|
172 |
|
173 | 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.
|
174 |
|
175 | Behavior:
|
176 | 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.
|
177 |
|
178 | ### Contributing
|
179 | Contributions are welcomed! Read the [Contributing Guide](./.github/CONTRIBUTING.md) for more information.
|
180 |
|
181 | ### Licensing
|
182 | This project is licensed under the Apache V2 License. See [LICENSE](LICENSE) for more information.
|