Skip to content

Commit 2a71e03

Browse files
add configurable timeout and retry delay for requests
1 parent 3804a83 commit 2a71e03

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/api.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import { readFileSync } from './util/fs'
1313

1414
const debug = Debug('api')
1515
let MAX_RETRY_LIMIT
16+
let RETRY_DELAY_BASE = 200 // Default base delay in milliseconds
17+
let TIMEOUT = 30000 // Default timeout in milliseconds
1618
let Contentstack
1719

1820
/**
1921
* @description Initialize sync utilities API requests
2022
* @param {Object} contentstack - Contentstack configuration details
21-
*/
23+
*/
2224
export const init = (contentstack) => {
2325
const packageInfo: any = JSON.parse(readFileSync(join(__dirname, '..', 'package.json')))
2426
Contentstack = contentstack
@@ -35,6 +37,14 @@ export const init = (contentstack) => {
3537
if (Contentstack.MAX_RETRY_LIMIT) {
3638
MAX_RETRY_LIMIT = Contentstack.MAX_RETRY_LIMIT
3739
}
40+
41+
if (Contentstack.RETRY_DELAY_BASE) {
42+
RETRY_DELAY_BASE = Contentstack.RETRY_DELAY_BASE
43+
}
44+
45+
if (Contentstack.TIMEOUT) {
46+
TIMEOUT = Contentstack.TIMEOUT
47+
}
3848
}
3949

4050
/**
@@ -60,7 +70,7 @@ export const get = (req, RETRY = 1) => {
6070
path: sanitizeUrl(encodeURI(req.path)),
6171
port: Contentstack.port,
6272
protocol: Contentstack.protocol,
63-
timeout: 30000, // 30 second timeout to prevent socket hang ups
73+
timeout: TIMEOUT, // Configurable timeout to prevent socket hang ups
6474
}
6575

6676
try {
@@ -77,8 +87,8 @@ export const get = (req, RETRY = 1) => {
7787
if (response.statusCode >= 200 && response.statusCode <= 399) {
7888
return resolve(JSON.parse(body))
7989
} else if (response.statusCode === 429) {
80-
timeDelay = Math.pow(Math.SQRT2, RETRY) * 200
81-
debug(`API rate limit exceeded. Retrying ${options.path} with ${timeDelay} sec delay`)
90+
timeDelay = Math.pow(Math.SQRT2, RETRY) * RETRY_DELAY_BASE
91+
debug(`API rate limit exceeded. Retrying ${options.path} with ${timeDelay} ms delay`)
8292

8393
return setTimeout(() => {
8494
return get(req, RETRY)
@@ -87,8 +97,8 @@ export const get = (req, RETRY = 1) => {
8797
}, timeDelay)
8898
} else if (response.statusCode >= 500) {
8999
// retry, with delay
90-
timeDelay = Math.pow(Math.SQRT2, RETRY) * 200
91-
debug(`Retrying ${options.path} with ${timeDelay} sec delay`)
100+
timeDelay = Math.pow(Math.SQRT2, RETRY) * RETRY_DELAY_BASE
101+
debug(`Retrying ${options.path} with ${timeDelay} ms delay`)
92102
RETRY++
93103

94104
return setTimeout(() => {
@@ -105,20 +115,20 @@ export const get = (req, RETRY = 1) => {
105115
})
106116

107117
// Set socket timeout to handle socket hang ups
108-
httpRequest.setTimeout(30000, () => {
109-
debug(`Request timeout for ${options.path}`)
118+
httpRequest.setTimeout(options.timeout, () => {
119+
debug(`Request timeout for ${options.path || 'unknown'}`)
110120
httpRequest.destroy()
111121
reject(new Error('Request timeout'))
112122
})
113123

114124
// Enhanced error handling for socket hang ups and connection resets
115125
httpRequest.on('error', (error: any) => {
116-
debug(`Request error for ${options.path}: ${error.message} (${error.code || 'NO_CODE'})`)
126+
debug(`Request error for ${options.path || 'unknown'}: ${error?.message || 'Unknown error'} (${error?.code || 'NO_CODE'})`)
117127

118128
// Handle socket hang up and connection reset errors with retry
119-
if ((error.code === 'ECONNRESET' || error.message?.includes('socket hang up')) && RETRY <= MAX_RETRY_LIMIT) {
120-
timeDelay = Math.pow(Math.SQRT2, RETRY) * 200
121-
debug(`Socket hang up detected. Retrying ${options.path} with ${timeDelay} ms delay (attempt ${RETRY}/${MAX_RETRY_LIMIT})`)
129+
if ((error?.code === 'ECONNRESET' || error?.message?.includes('socket hang up')) && RETRY <= MAX_RETRY_LIMIT) {
130+
timeDelay = Math.pow(Math.SQRT2, RETRY) * RETRY_DELAY_BASE
131+
debug(`Socket hang up detected. Retrying ${options.path || 'unknown'} with ${timeDelay} ms delay (attempt ${RETRY}/${MAX_RETRY_LIMIT})`)
122132
RETRY++
123133

124134
return setTimeout(() => {

src/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export const config = {
3333
},
3434
contentstack: {
3535
MAX_RETRY_LIMIT: 6,
36+
TIMEOUT: 30000, // 30 seconds - can be overridden by user config
37+
RETRY_DELAY_BASE: 200, // Base delay for retry logic - can be overridden by user config
3638
actions: {
3739
delete: 'delete',
3840
publish: 'publish',

0 commit comments

Comments
 (0)