Skip to content

Commit e4d583e

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

File tree

4 files changed

+176
-238
lines changed

4 files changed

+176
-238
lines changed

lib/definitions/rate-limit.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

lib/get-client.js

Lines changed: 11 additions & 42 deletions
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;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy