1
- const { memoize, get} = require ( 'lodash' ) ;
2
1
const { Octokit} = require ( '@octokit/rest' ) ;
3
2
const pRetry = require ( 'p-retry' ) ;
4
- const Bottleneck = require ( 'bottleneck' ) ;
5
3
const urljoin = require ( 'url-join' ) ;
6
4
const HttpProxyAgent = require ( 'http-proxy-agent' ) ;
7
5
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" ) ;
28
7
29
8
module . exports = ( { githubToken, githubUrl, githubApiPathPrefix, proxy} ) => {
30
9
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 ( {
33
12
auth : `token ${ githubToken } ` ,
34
13
baseUrl,
35
14
request : {
@@ -39,24 +18,14 @@ module.exports = ({githubToken, githubUrl, githubApiPathPrefix, proxy}) => {
39
18
: new HttpsProxyAgent ( proxy )
40
19
: undefined ,
41
20
} ,
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
+ } ,
60
29
} ) ;
61
30
62
31
return github ;
0 commit comments