forked from mirrors/probot
Hack to throttle requests
This commit is contained in:
parent
9c3ffb1839
commit
f4eb189687
14
lib/robot.js
14
lib/robot.js
|
@ -1,6 +1,7 @@
|
||||||
const bunyan = require('bunyan');
|
const bunyan = require('bunyan');
|
||||||
const bunyanFormat = require('bunyan-format');
|
const bunyanFormat = require('bunyan-format');
|
||||||
const GitHubApi = require('github');
|
const GitHubApi = require('github');
|
||||||
|
const Bottleneck = require('bottleneck');
|
||||||
const Context = require('./context');
|
const Context = require('./context');
|
||||||
|
|
||||||
const logger = bunyan.createLogger({
|
const logger = bunyan.createLogger({
|
||||||
|
@ -34,7 +35,7 @@ class Robot {
|
||||||
|
|
||||||
const github = new GitHubApi({debug: process.env.LOG_LEVEL === 'trace'});
|
const github = new GitHubApi({debug: process.env.LOG_LEVEL === 'trace'});
|
||||||
github.authenticate({type: 'token', token: token.token});
|
github.authenticate({type: 'token', token: token.token});
|
||||||
return github;
|
return rateLimitedClient(github);
|
||||||
}
|
}
|
||||||
|
|
||||||
log(...args) {
|
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
|
// Add level methods on the logger
|
||||||
['trace', 'debug', 'info', 'warn', 'error', 'fatal'].forEach(level => {
|
['trace', 'debug', 'info', 'warn', 'error', 'fatal'].forEach(level => {
|
||||||
Robot.prototype.log[level] = logger[level].bind(logger);
|
Robot.prototype.log[level] = logger[level].bind(logger);
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
"author": "Brandon Keepers",
|
"author": "Brandon Keepers",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"bottleneck": "^1.15.1",
|
||||||
"bunyan": "^1.8.5",
|
"bunyan": "^1.8.5",
|
||||||
"bunyan-format": "^0.2.1",
|
"bunyan-format": "^0.2.1",
|
||||||
"cache-manager": "^2.4.0",
|
"cache-manager": "^2.4.0",
|
||||||
|
|
Loading…
Reference in New Issue