Skip to content

Commit

Permalink
Improve secondary rate limit mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
serena97 committed Nov 26, 2021
1 parent b6f0af1 commit 2c04297
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
4 changes: 3 additions & 1 deletion src/commands/prs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const styles = require('../lib/styles')
const getConfig = require('../lib/config')
const promiseAllErrors = require('../lib/promise-all-errors')

const PR_CREATION_TIMEOUT_MS = 2000

exports.command = 'prs'
exports.desc = 'create Github pull requests for pushed branches'

Expand Down Expand Up @@ -82,7 +84,7 @@ exports.handler = async ({ templates: { title, body } }, state) => {
)} on ${styles.repo(`${repo.owner}/${repo.name}`)}`,
)
.then(response => response.data))
await new Promise(r => setTimeout(r, 2000))
await new Promise(r => setTimeout(r, PR_CREATION_TIMEOUT_MS))
}
}
}
Expand Down
37 changes: 17 additions & 20 deletions src/lib/octokit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,31 @@ const { retry } = require('@octokit/plugin-retry')
const OctokitInstance = Octokit.plugin(throttling, retry)
const logger = require('../lib/logger')

const RETRY_LIMIT = 5

let client

const retry = (retryAfter, options, message) => {
if (options.request.retryCount === RETRY_LIMIT) {
return
}
logger.log(`${message}, retrying after ${retryAfter}s`, {
status: 'pending',
message: `${message}, retrying after ${retryAfter}s`,
})
return true
}

module.exports = token => {
if (!client) {
client = new OctokitInstance({
previews: ['inertia-preview'],
auth: token,
throttle: {
onRateLimit: retryAfter => {
logger.log(
`Hit GitHub API rate limit, retrying after ${retryAfter}s`,
{
status: 'pending',
message: `Hit GitHub API rate limit, retrying after ${retryAfter}s`,
},
)
return true
},
onAbuseLimit: retryAfter => {
logger.log(
`Hit secondary GitHub API rate limit, retrying after ${retryAfter}s`,
{
status: 'pending',
message: `Hit secondary GitHub API rate limit, retrying after ${retryAfter}s`,
},
)
return true
},
onRateLimit: (retryAfter, options) =>
retry(retryAfter, options, 'Hit GitHub API rate limit'),
onAbuseLimit: (retryAfter, options) =>
retry(retryAfter, options, 'Hit secondary GitHub API rate limit'),
},
})
}
Expand Down

0 comments on commit 2c04297

Please sign in to comment.