Skip to content

Commit 48662dc

Browse files
author
naman-contentstack
committed
updated the delay logic
1 parent b0bdbff commit 48662dc

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

lib/core/concurrency-queue.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const defaultConfig = {
1414
retryOnHttpServerError: true,
1515
maxNetworkRetries: 3,
1616
networkRetryDelay: 100, // Base delay for network retries (ms)
17-
networkBackoffStrategy: 'exponential' // 'exponential' or 'fixed'
17+
networkBackoffStrategy: 'exponential', // 'exponential' or 'fixed'
18+
delayMs: null // Delay in milliseconds before making each request
1819
}
1920

2021
export function ConcurrencyQueue ({ axios, config }) {
@@ -175,16 +176,22 @@ export function ConcurrencyQueue ({ axios, config }) {
175176

176177
// Initial shift will check running request,
177178
// and adds request to running queue if max requests are not running
178-
this.initialShift = () => {
179+
this.initialShift = async () => {
179180
if (this.running.length < this.config.maxRequests && !this.paused) {
180-
shift()
181+
await shift()
181182
}
182183
}
183184

184185
// INTERNAL: Shift the queued item to running queue
185-
const shift = () => {
186+
const shift = async () => {
186187
if (this.queue.length && !this.paused) {
187188
const queueItem = this.queue.shift()
189+
190+
// Add configurable delay before making the request if specified
191+
if (this.config.delayMs && this.config.delayMs > 0) {
192+
await new Promise(resolve => setTimeout(resolve, this.config.delayMs))
193+
}
194+
188195
queueItem.resolve(queueItem.request)
189196
this.running.push(queueItem)
190197
}
@@ -197,7 +204,7 @@ export function ConcurrencyQueue ({ axios, config }) {
197204

198205
this.push = requestPromise => {
199206
this.queue.push(requestPromise)
200-
this.initialShift()
207+
this.initialShift().catch(console.error)
201208
}
202209

203210
this.clear = () => {
@@ -312,7 +319,7 @@ export function ConcurrencyQueue ({ axios, config }) {
312319
return refreshToken()
313320
} else {
314321
for (let i = 0; i < this.config.maxRequests; i++) {
315-
this.initialShift()
322+
this.initialShift().catch(console.error)
316323
}
317324
}
318325
}, time))
@@ -360,7 +367,7 @@ export function ConcurrencyQueue ({ axios, config }) {
360367
}
361368
})
362369
for (let i = 0; i < this.config.maxRequests; i++) {
363-
this.initialShift()
370+
this.initialShift().catch(console.error)
364371
}
365372
})
366373
}

lib/core/contentstackHTTPClient.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,7 @@ export default function contentstackHttpClient (options) {
109109
const instance = axios.create(axiosOptions)
110110
instance.httpClientParams = options
111111
instance.concurrencyQueue = new ConcurrencyQueue({ axios: instance, config })
112-
instance.interceptors.request.use(async (request) => {
113-
// Add configurable delay before making the request if specified
114-
if (config?.delayMs > 0) {
115-
await new Promise(resolve => setTimeout(resolve, config.delayMs))
116-
}
117-
112+
instance.interceptors.request.use((request) => {
118113
if (request.versioningStrategy && request.versioningStrategy === 'path') {
119114
request.baseURL = request.baseURL.replace('{api-version}', version)
120115
} else {

0 commit comments

Comments
 (0)