-
Notifications
You must be signed in to change notification settings - Fork 7
/
concurrency-queue.js
298 lines (275 loc) · 9.7 KB
/
concurrency-queue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import Axios from 'axios'
const defaultConfig = {
maxRequests: 5,
retryLimit: 5,
retryDelay: 300
}
export function ConcurrencyQueue ({ axios, config }) {
if (!axios) {
throw Error('Axios instance is not present')
}
if (config) {
if (config.maxRequests && config.maxRequests <= 0) {
throw Error('Concurrency Manager Error: minimum concurrent requests is 1')
} else if (config.retryLimit && config.retryLimit <= 0) {
throw Error('Retry Policy Error: minimum retry limit is 1')
} else if (config.retryDelay && config.retryDelay < 300) {
throw Error('Retry Policy Error: minimum retry delay for requests is 300')
}
}
this.config = Object.assign({}, defaultConfig, config)
this.queue = []
this.running = []
this.paused = false
// Initial shift will check running request,
// and adds request to running queue if max requests are not running
this.initialShift = () => {
if (this.running.length < this.config.maxRequests && !this.paused) {
shift()
}
}
// INTERNAL: Shift the queued item to running queue
const shift = () => {
if (this.queue.length && !this.paused) {
const queueItem = this.queue.shift()
queueItem.resolve(queueItem.request)
this.running.push(queueItem)
}
}
// Append the request at start of queue
this.unshift = requestPromise => {
this.queue.unshift(requestPromise)
}
this.push = requestPromise => {
this.queue.push(requestPromise)
this.initialShift()
}
this.clear = () => {
const requests = this.queue.splice(0, this.queue.length)
requests.forEach((element) => {
element.request.source.cancel()
})
}
// Detach the interceptors
this.detach = () => {
axios.interceptors.request.eject(this.interceptors.request)
axios.interceptors.response.eject(this.interceptors.response)
this.interceptors = {
request: null,
response: null
}
}
// Request interceptor to queue the request
const requestHandler = (request) => {
if (typeof request.data === 'function') {
request.formdata = request.data
request.data = transformFormData(request)
}
request.retryCount = request.retryCount || 0
if (request.headers.authorization && request.headers.authorization !== undefined) {
if (this.config.authorization && this.config.authorization !== undefined) {
request.headers.authorization = this.config.authorization
request.authorization = this.config.authorization
}
delete request.headers.authtoken
} else if (request.headers.authtoken && request.headers.authtoken !== undefined && this.config.authtoken && this.config.authtoken !== undefined) {
request.headers.authtoken = this.config.authtoken
request.authtoken = this.config.authtoken
}
if (request.cancelToken === undefined) {
const source = Axios.CancelToken.source()
request.cancelToken = source.token
request.source = source
}
if (this.paused && request.retryCount > 0) {
return new Promise((resolve, reject) => {
this.unshift({ request, resolve, reject })
})
} else if (request.retryCount > 0) {
return request
}
return new Promise((resolve, reject) => {
request.onComplete = () => {
this.running.pop({ request, resolve, reject })
}
this.push({ request, resolve, reject })
})
}
const delay = (time, isRefreshToken = false) => {
if (!this.paused) {
this.paused = true
// Check for current running request.
// Wait for running queue to complete.
// Wait and prosed the Queued request.
if (this.running.length > 0) {
setTimeout(() => {
delay(time, isRefreshToken)
}, time)
}
return new Promise(resolve => setTimeout(() => {
this.paused = false
if (isRefreshToken) {
return refreshToken()
} else {
for (let i = 0; i < this.config.maxRequests; i++) {
this.initialShift()
}
}
}, time))
}
}
const refreshToken = () => {
return config.refreshToken().then((token) => {
if (token.authorization) {
axios.defaults.headers.authorization = token.authorization
axios.defaults.authorization = token.authorization
axios.httpClientParams.authorization = token.authorization
axios.httpClientParams.headers.authorization = token.authorization
this.config.authorization = token.authorization
} else if (token.authtoken) {
axios.defaults.headers.authtoken = token.authtoken
axios.defaults.authtoken = token.authtoken
axios.httpClientParams.authtoken = token.authtoken
axios.httpClientParams.headers.authtoken = token.authtoken
this.config.authtoken = token.authtoken
}
}).catch((error) => {
this.queue.forEach(queueItem => {
queueItem.reject({
errorCode: '401',
errorMessage: (error instanceof Error) ? error.message : error,
code: 'Unauthorized',
message: 'Unable to refresh token',
name: 'Token Error',
config: queueItem.request,
stack: (error instanceof Error) ? error.stack : null,
})
})
this.queue = []
this.running = []
}).finally(() => {
this.queue.forEach((queueItem) => {
if (this.config.authorization) {
queueItem.request.headers.authorization = this.config.authorization
queueItem.request.authorization = this.config.authorization
}
if (this.config.authtoken) {
queueItem.request.headers.authtoken = this.config.authtoken
queueItem.request.authtoken = this.config.authtoken
}
})
for (let i = 0; i < this.config.maxRequests; i++) {
this.initialShift()
}
})
}
// Response interceptor used for
const responseHandler = (response) => {
response.config.onComplete()
shift()
return response
}
const responseErrorHandler = error => {
let networkError = error.config.retryCount
let retryErrorType = null
if (!this.config.retryOnError || networkError > this.config.retryLimit) {
return Promise.reject(responseHandler(error))
}
// Error handling
const wait = this.config.retryDelay
var response = error.response
if (!response) {
if (error.code === 'ECONNABORTED') {
error.response = {
...error.response,
status: 408,
statusText: `timeout of ${this.config.timeout}ms exceeded`
}
response = error.response
} else {
return Promise.reject(responseHandler(error))
}
} else if (response.status === 429 || (response.status === 401 && this.config.refreshToken)) {
retryErrorType = `Error with status: ${response.status}`
networkError++
if (networkError > this.config.retryLimit) {
return Promise.reject(responseHandler(error))
}
this.running.shift()
// Cool down the running requests
delay(wait, response.status === 401)
error.config.retryCount = networkError
// deepcode ignore Ssrf: URL is dynamic
return axios(updateRequestConfig(error, retryErrorType, wait))
}
if (this.config.retryCondition && this.config.retryCondition(error)) {
retryErrorType = error.response ? `Error with status: ${response.status}` : `Error Code:${error.code}`
networkError++
return this.retry(error, retryErrorType, networkError, wait)
}
return Promise.reject(responseHandler(error))
}
this.retry = (error, retryErrorType, retryCount, waittime) => {
let delaytime = waittime
if (retryCount > this.config.retryLimit) {
return Promise.reject(responseHandler(error))
}
if (this.config.retryDelayOptions) {
if (this.config.retryDelayOptions.customBackoff) {
delaytime = this.config.retryDelayOptions.customBackoff(retryCount, error)
if (delaytime && delaytime <= 0) {
return Promise.reject(responseHandler(error))
}
} else if (this.config.retryDelayOptions.base) {
delaytime = this.config.retryDelayOptions.base * retryCount
}
} else {
delaytime = this.config.retryDelay
}
error.config.retryCount = retryCount
return new Promise(function (resolve) {
return setTimeout(function () {
// deepcode ignore Ssrf: URL is dynamic
return resolve(axios(updateRequestConfig(error, retryErrorType, delaytime)))
}, delaytime)
})
}
this.interceptors = {
request: null,
response: null
}
const updateRequestConfig = (error, retryErrorType, wait) => {
const requestConfig = error.config
this.config.logHandler('warning', `${retryErrorType} error occurred. Waiting for ${wait} ms before retrying...`)
if (axios !== undefined && axios.defaults !== undefined) {
if (axios.defaults.agent === requestConfig.agent) {
delete requestConfig.agent
}
if (axios.defaults.httpAgent === requestConfig.httpAgent) {
delete requestConfig.httpAgent
}
if (axios.defaults.httpsAgent === requestConfig.httpsAgent) {
delete requestConfig.httpsAgent
}
}
requestConfig.data = transformFormData(requestConfig)
requestConfig.transformRequest = [function (data) {
return data
}]
return requestConfig
}
const transformFormData = (request) => {
if (request.formdata) {
const formdata = request.formdata()
request.headers = {
...request.headers,
...formdata.getHeaders()
}
return formdata
}
return request.data
}
// Adds interseptors in axios to queue request
this.interceptors.request = axios.interceptors.request.use(requestHandler)
this.interceptors.response = axios.interceptors.response.use(responseHandler, responseErrorHandler)
}