Skip to content

Commit 74e7752

Browse files
committed
fix(throttling): use Octokit's plugin-throttling instead of a custom throttling implementation
1 parent 14c2418 commit 74e7752

File tree

3 files changed

+12
-69
lines changed

3 files changed

+12
-69
lines changed

lib/definitions/rate-limit.js

-27
This file was deleted.

lib/get-client.js

+11-42
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,14 @@
1-
const {memoize, get} = require('lodash');
21
const {Octokit} = require('@octokit/rest');
32
const pRetry = require('p-retry');
4-
const Bottleneck = require('bottleneck');
53
const urljoin = require('url-join');
64
const HttpProxyAgent = require('http-proxy-agent');
75
const HttpsProxyAgent = require('https-proxy-agent');
8-
9-
const {RETRY_CONF, RATE_LIMITS, GLOBAL_RATE_LIMIT} = require('./definitions/rate-limit');
10-
11-
/**
12-
* Http error status for which to not retry.
13-
*/
14-
const SKIP_RETRY_CODES = new Set([400, 401, 403]);
15-
16-
/**
17-
* Create or retrieve the throttler function for a given rate limit group.
18-
*
19-
* @param {Array} rate The rate limit group.
20-
* @param {String} limit The rate limits per API endpoints.
21-
* @param {Bottleneck} globalThrottler The global throttler.
22-
*
23-
* @return {Bottleneck} The throller function for the given rate limit group.
24-
*/
25-
const getThrottler = memoize((rate, globalThrottler) =>
26-
new Bottleneck({minTime: get(RATE_LIMITS, rate)}).chain(globalThrottler)
27-
);
6+
const { throttling } = require("@octokit/plugin-throttling");
287

298
module.exports = ({githubToken, githubUrl, githubApiPathPrefix, proxy}) => {
309
const baseUrl = githubUrl && urljoin(githubUrl, githubApiPathPrefix);
31-
const globalThrottler = new Bottleneck({minTime: GLOBAL_RATE_LIMIT});
32-
const github = new Octokit({
10+
const OctokitWithThrottling = Octokit.plugin(throttling);
11+
const github = new OctokitWithThrottling({
3312
auth: `token ${githubToken}`,
3413
baseUrl,
3514
request: {
@@ -39,24 +18,14 @@ module.exports = ({githubToken, githubUrl, githubApiPathPrefix, proxy}) => {
3918
: new HttpsProxyAgent(proxy)
4019
: undefined,
4120
},
42-
});
43-
44-
github.hook.wrap('request', (request, options) => {
45-
const access = options.method === 'GET' ? 'read' : 'write';
46-
const rateCategory = options.url.startsWith('/search') ? 'search' : 'core';
47-
const limitKey = [rateCategory, RATE_LIMITS[rateCategory][access] && access].filter(Boolean).join('.');
48-
49-
return pRetry(async () => {
50-
try {
51-
return await getThrottler(limitKey, globalThrottler).wrap(request)(options);
52-
} catch (error) {
53-
if (SKIP_RETRY_CODES.has(error.status)) {
54-
throw new pRetry.AbortError(error);
55-
}
56-
57-
throw error;
58-
}
59-
}, RETRY_CONF);
21+
throttle: {
22+
onSecondaryRateLimit: (retryAfter, options, octokit) => {
23+
octokit.log.error("onSecondaryLimit", retryAfter, options);
24+
},
25+
onRateLimit: (retryAfter, options, octokit) => {
26+
octokit.log.error("onRateLimit", retryAfter, options);
27+
},
28+
},
6029
});
6130

6231
return github;

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"Gregor Martynus (https://twitter.com/gr2m)"
1717
],
1818
"dependencies": {
19+
"@octokit/plugin-throttling": "^3.6.2",
1920
"@octokit/rest": "^19.0.0",
2021
"@semantic-release/error": "^3.0.0",
2122
"aggregate-error": "^3.0.0",

0 commit comments

Comments
 (0)