Skip to content

Commit 5a9ce07

Browse files
committed
retry customBackoff update for not to retry if passed -1
1 parent 7dfd389 commit 5a9ce07

File tree

3 files changed

+102
-34
lines changed

3 files changed

+102
-34
lines changed

lib/contentstack.js

Lines changed: 92 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,101 @@ import httpClient from './core/contentstackHTTPClient.js'
1212
* Create client instance
1313
* @name client
1414
* @memberof Contentstack
15-
* @param {object} params - Client initialization parameters
16-
* @param {Object} param.proxy -
17-
* @prop {string} params.host - API host (default: api.contentstack.io)
18-
* @prop {object} params.headers - Optional additional headers
19-
* @prop {number} params.timeout - Optional number of milliseconds before the request times out. Default is 30000
20-
* @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.
25-
* @prop {number} params.maxContentLength - Optional maximum content length in bytes (default: 1073741824 i.e. 1GB)
26-
* @prop {string} params.application - Application name and version e.g myApp/version
27-
* @prop {string} params.integration - Integration name and version e.g react/version
28-
* @returns Contentstack.Client
15+
*
2916
* @example
3017
* import * as contentstack from '@contentstack/management'
3118
* const client = contentstack.client()
19+
*
20+
* @param {object} params - Client initialization parameters
21+
* @param {Object=} param.proxy -
22+
* @prop {string=} params.endpoint - API endpoint that a service will talk to
23+
* @example //Set the `endpoint` to 'https://api.contentstack.io:{port}/{version}'
24+
* import * as contentstack from '@contentstack/management'
25+
* const client = contentstack.client({ endpoint: 'https://api.contentstack.io:{port}/{version}' })
26+
*
27+
* @prop {string=} params.host - API host (default: api.contentstack.io)
28+
* @example //Set the `host` to 'api.contentstack.io'
29+
* import * as contentstack from '@contentstack/management'
30+
* const client = contentstack.client({ host: 'api.contentstack.io' })
31+
*
32+
* @prop {object=} params.headers - Optional additional headers
33+
* @example //Set the `headers` to { 'headerkey': 'value'}
34+
* import * as contentstack from '@contentstack/management'
35+
* const client = contentstack.client({ headers: { 'headerkey': 'value'} })
36+
*
37+
* @prop {number=} params.timeout - Optional number of milliseconds before the request times out. Default is 30000ms
38+
* @example //Set the `timeout` to 50000ms
39+
* import * as contentstack from '@contentstack/management'
40+
* const client = contentstack.client({ timeout: 50000 })
41+
*
42+
* @prop {boolean=} params.retryOnError - Optional boolean for retry on failuer. Default is true
43+
* @example //Set the `retryOnError` to false
44+
* import * as contentstack from '@contentstack/management'
45+
* const client = contentstack.client({ retryOnError: false })
46+
*
47+
* @prop {number=} params.retryLimit - Optional number of retries before failure. Default is 5
48+
* @example //Set the `retryLimit` to 2
49+
* import * as contentstack from '@contentstack/management'
50+
* const client = contentstack.client({ retryLimit: 2 })
51+
*
52+
* @prop {number=} params.retryDelay - The number of miliseconds to use for operation retries. Default is 300ms
53+
* @example //Set the `retryDelay` to 500ms
54+
* import * as contentstack from '@contentstack/management'
55+
* const client = contentstack.client({ retryDelay: 500 })
56+
*
57+
* @prop {Function=} params.retryCondition - A function to determine if the error can be retried. Default retry is on status 429.
58+
* @example //Set the `retryCondition` on error status 429
59+
* import * as contentstack from '@contentstack/management'
60+
* const client = contentstack.client({ retryCondition: (error) => {
61+
* if (error.response && error.response.status === 429) {
62+
* return true
63+
* }
64+
* return false
65+
* }
66+
* })
67+
*
68+
* @prop {number=} params.retryDelayOptions.base - The base number of milliseconds to use in the exponential backoff for operation retries.
69+
* @example Set base retry delay for all services to 300 ms
70+
* import * as contentstack from '@contentstack/management'
71+
* const client = contentstack.client({retryDelayOptions: {base: 300}})
72+
*
73+
* @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. (if you want not to retry for specific condition return -1)
74+
* @example Set a custom backoff function to provide delay of 500 ms on retryCount < 3 and -1 for retryCount >= 3values on retries
75+
* import * as contentstack from '@contentstack/management'
76+
* const client = contentstack.client({retryDelayOptions: {customBackoff: function(retryCount, err) {
77+
* if (retryCount < 3) {
78+
* return 500
79+
* } else {
80+
* return -1 //returning -1 will hold next retry for request
81+
* }
82+
* }}}
83+
* )
84+
*
85+
* @prop {number=} params.maxContentLength - Optional maximum content length in bytes (default: 1073741824 i.e. 1 GB)
86+
* @example //Set the `maxContentLength` to 1024 ** 3
87+
* import * as contentstack from '@contentstack/management'
88+
* const client = contentstack.client({ maxContentLength: 1024 ** 3 })
89+
*
90+
* @prop {number=} params.maxBodyLength - Optional maximum body length in bytes (default: 10 MB)
91+
* @example //Set the `maxContentLength` to 1024 ** 2 * 10 // 10 MB
92+
* import * as contentstack from '@contentstack/management'
93+
* const client = contentstack.client({ maxBodyLength: 1024 ** 2 * 10 })
94+
*
95+
* @prop {function=} params.logHandler - A log handler function to process given log messages & errors.
96+
* @example //Set the `logHandler`
97+
* import * as contentstack from '@contentstack/management'
98+
* const client = contentstack.client({ logHandler: (level, data) => {
99+
if (level === 'error' && data) {
100+
const title = [data.name, data.message].filter((a) => a).join(' - ')
101+
console.error(`[error] ${title}`)
102+
return
103+
}
104+
console.log(`[${level}] ${data}`)
105+
} })
106+
*
107+
* @prop {string=} params.application - Application name and version e.g myApp/version
108+
* @prop {string=} params.integration - Integration name and version e.g react/version
109+
* @returns Contentstack.Client
32110
*/
33111
export function client (params = {}) {
34112
const defaultParameter = {

lib/core/contentstack-retry.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,21 @@ export default function contentstckRetry (axios, defaultOptions, retryLimit = 5,
2323
return Promise.reject(error)
2424
}
2525
if (defaultOptions.retryCondition && defaultOptions.retryCondition(error)) {
26-
retryErrorType = 'Rate Limit'
26+
retryErrorType = `Error with status: ${error.response.status}`
2727
networkError++
2828
if (networkError > retryLimit) {
2929
networkError = 0
3030
return Promise.reject(error)
3131
}
3232
if (defaultOptions.retryDelayOptions) {
33-
if (defaultOptions.retryDelayOptions.base) {
34-
wait = defaultOptions.retryDelayOptions.base * networkError
35-
} else if (defaultOptions.retryDelayOptions.customBackoff) {
33+
if (defaultOptions.retryDelayOptions.customBackoff) {
3634
wait = defaultOptions.retryDelayOptions.customBackoff(networkError, error)
35+
if (wait && wait <= 0) {
36+
networkError = 0
37+
return Promise.reject(error)
38+
}
39+
} else if (defaultOptions.retryDelayOptions.base) {
40+
wait = defaultOptions.retryDelayOptions.base * networkError
3741
}
3842
} else {
3943
wait = retryDelay

lib/core/contentstackHTTPClient.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,11 @@ export default function contentstackHttpClient (options) {
6060
if (config.basePath) {
6161
config.basePath = `/${config.basePath.split('/').filter(Boolean).join('/')}`
6262
}
63-
const baseURL = `${protocol}://${hostname}:${port}${config.basePath}/${version}`
63+
const baseURL = config.endpoint || `${protocol}://${hostname}:${port}${config.basePath}/${version}`
6464
const axiosOptions = {
6565
// Axios
6666
baseURL,
67-
headers: config.headers,
68-
httpAgent: config.httpAgent,
69-
httpsAgent: config.httpsAgent,
70-
proxy: config.proxy,
71-
timeout: config.timeout,
72-
adapter: config.adapter,
73-
maxContentLength: config.maxContentLength,
74-
maxBodyLength: config.maxBodyLength,
75-
// Contentstack
76-
logHandler: config.logHandler,
77-
responseLogger: config.responseLogger,
78-
requestLogger: config.requestLogger,
79-
retryOnError: config.retryOnError,
80-
retryDelayOptions: config.retryDelayOptions,
81-
retryCondition: config.retryCondition,
67+
...config,
8268
paramsSerializer: function (params) {
8369
var query = params.query
8470
delete params.query

0 commit comments

Comments
 (0)