@@ -229,6 +229,7 @@ export function ConcurrencyQueue ({ axios, config }) {
229
229
if ( ! this . config . retryOnError || networkError > this . config . retryLimit ) {
230
230
return Promise . reject ( responseHandler ( error ) )
231
231
}
232
+ // Check rate limit remaining header before retrying
232
233
233
234
// Error handling
234
235
const wait = this . config . retryDelay
@@ -244,19 +245,26 @@ export function ConcurrencyQueue ({ axios, config }) {
244
245
} else {
245
246
return Promise . reject ( responseHandler ( error ) )
246
247
}
247
- } else if ( ( response . status === 401 && this . config . refreshToken ) ) {
248
- retryErrorType = `Error with status: ${ response . status } `
249
- networkError ++
250
-
251
- if ( networkError > this . config . retryLimit ) {
248
+ } else {
249
+ const rateLimitRemaining = response . headers [ 'x-ratelimit-remaining' ]
250
+ if ( rateLimitRemaining !== undefined && parseInt ( rateLimitRemaining ) <= 0 ) {
252
251
return Promise . reject ( responseHandler ( error ) )
253
252
}
254
- this . running . shift ( )
255
- // Cool down the running requests
256
- delay ( wait , response . status === 401 )
257
- error . config . retryCount = networkError
258
- // deepcode ignore Ssrf: URL is dynamic
259
- return axios ( updateRequestConfig ( error , retryErrorType , wait ) )
253
+
254
+ if ( ( response . status === 401 && this . config . refreshToken ) ) {
255
+ retryErrorType = `Error with status: ${ response . status } `
256
+ networkError ++
257
+
258
+ if ( networkError > this . config . retryLimit ) {
259
+ return Promise . reject ( responseHandler ( error ) )
260
+ }
261
+ this . running . shift ( )
262
+ // Cool down the running requests
263
+ delay ( wait , response . status === 401 )
264
+ error . config . retryCount = networkError
265
+ // deepcode ignore Ssrf: URL is dynamic
266
+ return axios ( updateRequestConfig ( error , retryErrorType , wait ) )
267
+ }
260
268
}
261
269
if ( this . config . retryCondition && this . config . retryCondition ( error ) ) {
262
270
retryErrorType = error . response ? `Error with status: ${ response . status } ` : `Error Code:${ error . code } `
0 commit comments