From f4eb18968772e5d6b815ac5661026af7ebffc2b4 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Mon, 27 Mar 2017 23:57:23 -0500 Subject: [PATCH] Hack to throttle requests --- lib/robot.js | 14 +++++++++++++- package.json | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/robot.js b/lib/robot.js index 88e489d4..9256f8fa 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -1,6 +1,7 @@ const bunyan = require('bunyan'); const bunyanFormat = require('bunyan-format'); const GitHubApi = require('github'); +const Bottleneck = require('bottleneck'); const Context = require('./context'); const logger = bunyan.createLogger({ @@ -34,7 +35,7 @@ class Robot { const github = new GitHubApi({debug: process.env.LOG_LEVEL === 'trace'}); github.authenticate({type: 'token', token: token.token}); - return github; + return rateLimitedClient(github); } log(...args) { @@ -42,6 +43,17 @@ class Robot { } } +// Hack client to only allow one request at a time with a 1s delay +// https://github.com/mikedeboer/node-github/issues/526 +function rateLimitedClient(github) { + const limiter = new Bottleneck(1, 1000); + const oldHandler = github.handler; + github.handler = (msg, block, callback) => { + limiter.submit(oldHandler.bind(github), msg, block, callback); + }; + return github; +} + // Add level methods on the logger ['trace', 'debug', 'info', 'warn', 'error', 'fatal'].forEach(level => { Robot.prototype.log[level] = logger[level].bind(logger); diff --git a/package.json b/package.json index 948ba779..1f28a8d7 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "author": "Brandon Keepers", "license": "ISC", "dependencies": { + "bottleneck": "^1.15.1", "bunyan": "^1.8.5", "bunyan-format": "^0.2.1", "cache-manager": "^2.4.0",