index: add option to disable rate limiter entirely

for the case that insight-api is used in an internal network
where rate limiting isn't necessary
This commit is contained in:
Braydon Fuller 2016-06-07 11:02:58 -04:00
parent b9c909ad40
commit 57e65010f1
2 changed files with 55 additions and 2 deletions

View File

@ -47,6 +47,7 @@ var InsightAPI = function(options) {
this.cacheLongSeconds = options.cacheLongSeconds;
this.rateLimiterOptions = options.rateLimiterOptions;
this.disableRateLimiter = options.disableRateLimiter;
this.blockSummaryCacheSize = options.blockSummaryCacheSize || BlockController.DEFAULT_BLOCKSUMMARY_CACHE_SIZE;
this.blockCacheSize = options.blockCacheSize || BlockController.DEFAULT_BLOCK_CACHE_SIZE;
@ -130,8 +131,10 @@ InsightAPI.prototype.setupRoutes = function(app) {
var self = this;
//Enable rate limiter
var limiter = this._getRateLimiter();
app.use(limiter.middleware());
if (!this.disableRateLimiter) {
var limiter = this._getRateLimiter();
app.use(limiter.middleware());
}
//Setup logging
morgan.token('remote-forward-addr', function(req){

View File

@ -15,6 +15,14 @@ describe('Index', function() {
});
index.rateLimiterOptions.should.equal(options);
});
it('will set disable rate limiter option', function() {
var node = {};
var index = new InsightAPI({
disableRateLimiter: true,
node: node
});
index.disableRateLimiter.should.equal(true);
});
});
describe('#_getRateLimiter', function() {
it('will pass options to rate limiter', function() {
@ -156,4 +164,46 @@ describe('Index', function() {
});
});
});
describe('#setupRoutes', function() {
it('will use rate limiter by default', function() {
var node = {};
var index = new InsightAPI({
node: node
});
var middlewareFunc = sinon.stub();
var middleware = sinon.stub().returns(middlewareFunc);
var limiter = {
middleware: middleware
};
index._getRateLimiter = sinon.stub().returns(limiter);
var use = sinon.stub();
var app = {
use: use,
get: sinon.stub(),
param: sinon.stub(),
post: sinon.stub()
};
index.setupRoutes(app);
use.callCount.should.be.above(0);
use.args[0][0].should.equal(middlewareFunc);
middleware.callCount.should.equal(1);
});
it('will NOT use rate limiter if disabled', function() {
var node = {};
var index = new InsightAPI({
node: node,
disableRateLimiter: true
});
index._getRateLimiter = sinon.stub();
var use = sinon.stub();
var app = {
use: use,
get: sinon.stub(),
param: sinon.stub(),
post: sinon.stub()
};
index.setupRoutes(app);
index._getRateLimiter.callCount.should.equal(0);
});
});
});