Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

Commit 6f10b35

Browse files
committed
Tweak rate limit failure mode
1 parent a36b1d3 commit 6f10b35

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/open-api/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ exports.generateMethod = method => {
122122
const MAX_RETRY = 10
123123
const DEFAULT_RETRY_DELAY = 5000 //ms
124124

125+
let last_retry_delay = DEFAULT_RETRY_DELAY
125126
for (let index = 0; index <= MAX_RETRY; index++) {
126127
debug('Rate limit attempt ' + index + ' for ' + path)
127128
const response = await makeRequest()
128-
if (index === MAX_RETRY) throw new Error('Rate limit retry exhausted, aborting ')
129-
if (http.STATUS_CODES[response.status] !== 'Too Many Requests') {
129+
if (http.STATUS_CODES[response.status] !== 'Too Many Requests' || index === MAX_RETRY) {
130+
if (index === MAX_RETRY) debug(`Rate limit retry exhausted, aborting request...`)
130131
return response
131132
} else {
132-
debug(`Rate limited, retrying`)
133133
try {
134134
const rateLimitReset = response.headers.get('X-RateLimit-Reset')
135135
const resetTime = Number.parseInt(rateLimitReset)
@@ -138,12 +138,12 @@ exports.generateMethod = method => {
138138
throw new Error('Header missing reset time')
139139
}
140140
const now = unixNow()
141-
const sleepTime = ((resetTime - now < 0 ? 0 : resetTime - now) + 1) * 1000 // minimum 1 second
142-
debug(`sleeping for ${sleepTime}ms`)
143-
await sleep(sleepTime)
141+
last_retry_delay = ((resetTime - now < 0 ? 0 : resetTime - now) + 1) * 1000 // minimum 1 second
142+
debug(`sleeping for ${last_retry_delay}ms`)
143+
await sleep(last_retry_delay)
144144
} catch (e) {
145-
debug(`sleeping for ${DEFAULT_RETRY_DELAY}ms`)
146-
await sleep(DEFAULT_RETRY_DELAY) // Default to 5 seconds if the header is borked
145+
debug(`sleeping for ${last_retry_delay}ms`)
146+
await sleep(last_retry_delay)
147147
}
148148
}
149149
}

0 commit comments

Comments
 (0)