Hack to throttle requests

This commit is contained in:
Brandon Keepers 2017-03-27 23:57:23 -05:00
parent 9c3ffb1839
commit f4eb189687
No known key found for this signature in database
GPG Key ID: F9533396D5FACBF6
2 changed files with 14 additions and 1 deletions

View File

@ -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);

View File

@ -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",