Skip to content

Commit

Permalink
Implement rate limiting (#331)
Browse files Browse the repository at this point in the history
This will automatically wait until the right time. Magic!

Fixes #330.
  • Loading branch information
fwouts authored Aug 11, 2019
1 parent c8641a9 commit eceaaf6
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@fortawesome/fontawesome-svg-core": "^1.2.21",
"@fortawesome/free-solid-svg-icons": "^5.10.1",
"@fortawesome/react-fontawesome": "^0.1.4",
"@octokit/plugin-throttling": "^2.6.0",
"@octokit/rest": "^16.28.7",
"@types/lodash": "^4.14.136",
"assert-never": "^1.2.0",
Expand Down
37 changes: 35 additions & 2 deletions src/github-api/implementation.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,45 @@
import Octokit from "@octokit/rest";
import { GitHubApi } from "./api";

const ThrottledOctokit = Octokit.plugin(require("@octokit/plugin-throttling"));

interface ThrottlingOptions {
method: string;
url: string;
request: {
retryCount: number;
};
}

export function buildGitHubApi(token: string): GitHubApi {
const octokit = new Octokit({
const octokit = new ThrottledOctokit({
auth: `token ${token}`,
// https://developer.github.com/v3/pulls/#list-pull-requests
// Enable Draft Pull Request API.
previews: ["shadow-cat"]
previews: ["shadow-cat"],
throttle: {
onRateLimit: (retryAfterSeconds: number, options: ThrottlingOptions) => {
console.warn(
`Request quota exhausted for request ${options.method} ${options.url}`
);
// Only retry twice.
if (options.request.retryCount < 2) {
console.log(`Retrying after ${retryAfterSeconds} seconds!`);
return true;
}
return false;
},
onAbuseLimit: (
_retryAfterSeconds: number,
options: ThrottlingOptions
) => {
// Does not retry, only logs a warning.
console.warn(
`Abuse detected for request ${options.method} ${options.url}`
);
return false;
}
}
});
return {
async loadAuthenticatedUser() {
Expand Down
5 changes: 3 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require("path");
const webpack = require("webpack");
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
.BundleAnalyzerPlugin;
const CopyPlugin = require("copy-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");

Expand Down Expand Up @@ -35,7 +36,7 @@ module.exports = {
popup: "./src/popup.tsx"
},
resolve: {
extensions: [".tsx", ".ts", ".js"]
extensions: [".tsx", ".ts", ".js", ".json"]
},
output: {
filename: "[name].js",
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,13 @@
universal-user-agent "^3.0.0"
url-template "^2.0.8"

"@octokit/plugin-throttling@^2.6.0":
version "2.6.0"
resolved "https://registry.yarnpkg.com/@octokit/plugin-throttling/-/plugin-throttling-2.6.0.tgz#0b3107c2267e3a8cf6f63925847ec3ebe9e0aa8d"
integrity sha512-E0xQrcD36sVEeBhut6j9nWX38vm/1LKMRSUqjvJ/mqGLXfHr4jYMsrR3I/nT2QC0eJL1/SKMt7zxOt7pZiFhDA==
dependencies:
bottleneck "^2.15.3"

"@octokit/request-error@^1.0.1", "@octokit/request-error@^1.0.2":
version "1.0.4"
resolved "https://registry.yarnpkg.com/@octokit/request-error/-/request-error-1.0.4.tgz#15e1dc22123ba4a9a4391914d80ec1e5303a23be"
Expand Down Expand Up @@ -1828,6 +1835,11 @@ bootstrap@^4.3.1:
resolved "https://registry.yarnpkg.com/bootstrap/-/bootstrap-4.3.1.tgz#280ca8f610504d99d7b6b4bfc4b68cec601704ac"
integrity sha512-rXqOmH1VilAt2DyPzluTi2blhk17bO7ef+zLLPlWvG494pDxcM234pJ8wTc/6R40UWizAIIMgxjvxZg5kmsbag==

bottleneck@^2.15.3:
version "2.19.5"
resolved "https://registry.yarnpkg.com/bottleneck/-/bottleneck-2.19.5.tgz#5df0b90f59fd47656ebe63c78a98419205cadd91"
integrity sha512-VHiNCbI1lKdl44tGrhNfU3lup0Tj/ZBMJB5/2ZbNXRCPuRCO7ed2mgcK4r17y+KB2EfuYuRaVlwNbAeaWGSpbw==

brace-expansion@^1.1.7:
version "1.1.11"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
Expand Down

0 comments on commit eceaaf6

Please sign in to comment.