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 3 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 = {
retries: 3,
retryAfterBaseValue: 1000,
doNotRetry: [400, 401, 403],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After switching to your branch (thanks for that) we still see rate-limits, but they always seem to return with a 403.

Any reason why 403s where excluded?

image

Copy link

@tractorcow tractorcow Mar 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, maybe that's the issue I ran into. I noticed that rate limiting wasn't retrying. I should look at those bugs in the core retry plugin to see if those can be patched.

Copy link

@johanneswuerbach johanneswuerbach Mar 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if you still use the instruction provided in #487 (comment), but for us it started to work after actually defining an override in our package.json:

"overrides": {
    "semantic-release": {
      "@semantic-release/github": "$@semantic-release/github"
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the secondary rate limit error is caused by this: octokit/plugin-throttling.js#454

This may be a short-term solution - #487 (comment) but I think it'll turn off the (rather nice) feature that adds a comment to each PR in a given release saying "this PR is included in release version XXX"

};

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 retries.
achingbrain marked this conversation as resolved.
Show resolved Hide resolved
*/
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