Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use retry and throttle octokit plugins #487

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 0 additions & 27 deletions lib/definitions/rate-limit.js

This file was deleted.

10 changes: 10 additions & 0 deletions lib/definitions/retry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Default exponential backoff configuration for retries.
*/
const RETRY_CONF = {
// By default, Octokit does not retry on 404s.
// But we want to retry on 404s to account for replication lag.
doNotRetry: [400, 401, 403, 422],
};

module.exports = {RETRY_CONF};
6 changes: 6 additions & 0 deletions lib/definitions/throttle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Default exponential backoff configuration for throttle.
*/
const THROTTLE_CONF = {retryAfterBaseValue: 1000};

module.exports = {THROTTLE_CONF};
62 changes: 20 additions & 42 deletions lib/get-client.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,40 @@
const {memoize, get} = require('lodash');
const {Octokit} = require('@octokit/rest');
const pRetry = require('p-retry');
const Bottleneck = require('bottleneck');
const urljoin = require('url-join');
const HttpProxyAgent = require('http-proxy-agent');
const HttpsProxyAgent = require('https-proxy-agent');
const {throttling} = require('@octokit/plugin-throttling');
const {RETRY_CONF} = require('./definitions/retry');
const {THROTTLE_CONF} = require('./definitions/throttle');
const {retry} = require('@octokit/plugin-retry');

const {RETRY_CONF, RATE_LIMITS, GLOBAL_RATE_LIMIT} = require('./definitions/rate-limit');

/**
* Http error status for which to not retry.
*/
const SKIP_RETRY_CODES = new Set([400, 401, 403]);
module.exports = ({githubToken, githubUrl, githubApiPathPrefix, proxy}) => {
const onRetry = (retryAfter, options) => {
github.log.warn(`Request quota exhausted for request ${options.method} ${options.url}`);

/**
* Create or retrieve the throttler function for a given rate limit group.
*
* @param {Array} rate The rate limit group.
* @param {String} limit The rate limits per API endpoints.
* @param {Bottleneck} globalThrottler The global throttler.
*
* @return {Bottleneck} The throller function for the given rate limit group.
*/
const getThrottler = memoize((rate, globalThrottler) =>
new Bottleneck({minTime: get(RATE_LIMITS, rate)}).chain(globalThrottler)
);
if (options.request.retryCount <= RETRY_CONF.retries) {
github.log.debug(`Will retry after ${retryAfter}.`);
return true;
}
};

module.exports = ({githubToken, githubUrl, githubApiPathPrefix, proxy}) => {
const baseUrl = githubUrl && urljoin(githubUrl, githubApiPathPrefix);
const globalThrottler = new Bottleneck({minTime: GLOBAL_RATE_LIMIT});
const github = new Octokit({
const OctokitWithThrottlingAndRetry = Octokit.plugin(throttling, retry);
const github = new OctokitWithThrottlingAndRetry({
auth: `token ${githubToken}`,
baseUrl,
retry: RETRY_CONF,
request: {
agent: proxy
? baseUrl && new URL(baseUrl).protocol.replace(':', '') === 'http'
? new HttpProxyAgent(proxy)
: new HttpsProxyAgent(proxy)
: undefined,
},
});

github.hook.wrap('request', (request, options) => {
const access = options.method === 'GET' ? 'read' : 'write';
const rateCategory = options.url.startsWith('/search') ? 'search' : 'core';
const limitKey = [rateCategory, RATE_LIMITS[rateCategory][access] && access].filter(Boolean).join('.');

return pRetry(async () => {
try {
return await getThrottler(limitKey, globalThrottler).wrap(request)(options);
} catch (error) {
if (SKIP_RETRY_CODES.has(error.status)) {
throw new pRetry.AbortError(error);
}

throw error;
}
}, RETRY_CONF);
throttle: {
...THROTTLE_CONF,
onSecondaryRateLimit: onRetry,
onRateLimit: onRetry,
},
});

return github;
Expand Down
Loading