Skip to content

Commit 7dfd389

Browse files
committed
Retry functionality update
- Retry Delay with base and conditional backoff added - Retry on error Condition added
1 parent 691054b commit 7dfd389

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

lib/contentstack.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import httpClient from './core/contentstackHTTPClient.js'
1818
* @prop {object} params.headers - Optional additional headers
1919
* @prop {number} params.timeout - Optional number of milliseconds before the request times out. Default is 30000
2020
* @prop {number} params.retryLimit - Optional number of retries before failure. Default is 5
21+
* @prop {number} params.retryDelay - The number of miliseconds to use for operation retries. Default is 300ms
22+
* @prop {Function} params.retryCondition - A function to determine if the error can be retried. Default retry is on status 429.
23+
* @prop {number} params.retryDelayOptions.base - The base number of milliseconds to use in the exponential backoff for operation retries.
24+
* @prop {Function} params.retryDelayOptions.customBackoff - A custom function that accepts a retry count and error and returns the amount of time to delay in milliseconds.
2125
* @prop {number} params.maxContentLength - Optional maximum content length in bytes (default: 1073741824 i.e. 1GB)
2226
* @prop {string} params.application - Application name and version e.g myApp/version
2327
* @prop {string} params.integration - Integration name and version e.g react/version

lib/core/contentstack-retry.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
export default function contentstckRetry (axios, defaultOptions, retryLimit = 5, retryDelay = 2) {
2+
export default function contentstckRetry (axios, defaultOptions, retryLimit = 5, retryDelay = 300) {
33
var networkError = 0
44
axios.interceptors.request.use(function (config) {
55
if (config.headers.authorization && config.headers.authorization !== undefined) {
@@ -22,15 +22,22 @@ export default function contentstckRetry (axios, defaultOptions, retryLimit = 5,
2222
retryErrorType = `Server connection`
2323
return Promise.reject(error)
2424
}
25-
26-
if (response && response.status === 429) {
25+
if (defaultOptions.retryCondition && defaultOptions.retryCondition(error)) {
2726
retryErrorType = 'Rate Limit'
2827
networkError++
2928
if (networkError > retryLimit) {
3029
networkError = 0
3130
return Promise.reject(error)
3231
}
33-
wait = retryDelay
32+
if (defaultOptions.retryDelayOptions) {
33+
if (defaultOptions.retryDelayOptions.base) {
34+
wait = defaultOptions.retryDelayOptions.base * networkError
35+
} else if (defaultOptions.retryDelayOptions.customBackoff) {
36+
wait = defaultOptions.retryDelayOptions.customBackoff(networkError, error)
37+
}
38+
} else {
39+
wait = retryDelay
40+
}
3441
} else {
3542
networkError = 0
3643
}

lib/core/contentstackHTTPClient.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ export default function contentstackHttpClient (options) {
1616
}
1717
console.log(`[${level}] ${data}`)
1818
},
19+
retryCondition: (error) => {
20+
if (error.response && error.response.status === 429) {
21+
return true
22+
}
23+
return false
24+
},
1925
headers: {},
2026
basePath: '',
2127
proxy: false,
@@ -71,6 +77,8 @@ export default function contentstackHttpClient (options) {
7177
responseLogger: config.responseLogger,
7278
requestLogger: config.requestLogger,
7379
retryOnError: config.retryOnError,
80+
retryDelayOptions: config.retryDelayOptions,
81+
retryCondition: config.retryCondition,
7482
paramsSerializer: function (params) {
7583
var query = params.query
7684
delete params.query

0 commit comments

Comments
 (0)