feat: new throttling & automated retries

This commit is contained in:
Gregor 2019-01-08 18:34:47 -08:00
parent 39f1fc087a
commit c6cbc97bf0
6 changed files with 23 additions and 32 deletions

View File

@ -0,0 +1,5 @@
declare module '@octokit/plugin-retry' {
import Octokit from '@octokit/rest'
declare function plugin(octokit: Octokit, options: Octokit.Options): void
export = plugin
}

View File

@ -0,0 +1,5 @@
declare module '@octokit/plugin-throttling' {
import Octokit from '@octokit/rest'
declare function plugin(octokit: Octokit, options: Octokit.Options): void
export = plugin
}

View File

@ -1,8 +1,14 @@
import retryPlugin from '@octokit/plugin-retry'
import throttlePlugin from '@octokit/plugin-throttling'
import Octokit from '@octokit/rest'
import { addGraphQL } from './graphql'
import { addLogging, Logger } from './logging'
import { addPagination } from './pagination'
import { addRateLimiting } from './rate-limiting'
const ProbotOctokit = Octokit
.plugin(throttlePlugin)
.plugin(retryPlugin)
/**
* the [@octokit/rest Node.js module](https://github.com/octokit/rest.js),
@ -12,10 +18,14 @@ import { addRateLimiting } from './rate-limiting'
* @see {@link https://github.com/octokit/rest.js}
*/
export function GitHubAPI (options: Options = {} as any) {
const octokit = new Octokit(options) as GitHubAPI
const octokit = new ProbotOctokit(Object.assign(options, {
throttle: {
onAbuseLimit: (retryAfter: number) => options.logger.warn(`Abuse limit hit, retrying in ${retryAfter} seconds`),
onRateLimit: (retryAfter: number) => options.logger.warn(`Rate limit hit, retrying in ${retryAfter} seconds`)
}
})) as GitHubAPI
addPagination(octokit)
addRateLimiting(octokit, options.limiter)
addLogging(octokit, options.logger)
addGraphQL(octokit)
@ -25,7 +35,6 @@ export function GitHubAPI (options: Options = {} as any) {
export interface Options extends Octokit.Options {
debug?: boolean
logger: Logger
limiter?: any
}
export interface RequestOptions {

View File

@ -1,14 +0,0 @@
import Bottleneck from 'bottleneck'
import { GitHubAPI } from './'
export function addRateLimiting (octokit: GitHubAPI, limiter: Bottleneck) {
if (!limiter) {
limiter = new Bottleneck({
maxConcurrent: 1,
minTime: 1000
})
}
const noop = () => Promise.resolve()
octokit.hook.before('request', () => limiter.schedule(noop))
}

View File

@ -1,4 +1,3 @@
import Bottleneck from 'bottleneck'
import nock from 'nock'
import { GitHubAPI, Options } from '../src/github'
import { logger } from '../src/logger'
@ -7,11 +6,7 @@ describe('GitHubAPI', () => {
let github: GitHubAPI
beforeEach(() => {
// Set a shorter limiter, otherwise tests are _slow_
const limiter = new Bottleneck()
const options: Options = {
limiter,
logger
}

View File

@ -1,4 +1,3 @@
import Bottleneck from 'bottleneck'
import nock from 'nock'
import { GitHubAPI, Options } from '../../src/github'
import { logger } from '../../src/logger'
@ -11,11 +10,7 @@ describe('github/graphql', () => {
afterEach(() => expect(nock.pendingMocks()).toEqual([]))
beforeEach(() => {
// Set a shorter limiter, otherwise tests are _slow_
const limiter = new Bottleneck()
const options: Options = {
limiter,
logger
}
@ -97,11 +92,7 @@ describe('github/graphql', () => {
beforeEach(() => {
process.env.GHE_HOST = 'notreallygithub.com'
// Set a shorter limiter, otherwise tests are _slow_
const limiter = new Bottleneck()
const options: Options = {
limiter,
logger
}