Skip to content

Commit

Permalink
throttle updateData to prevent loading way too many times
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoncalabrese committed Jul 18, 2015
1 parent af3a67b commit 99ea9fa
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
10 changes: 7 additions & 3 deletions lib/bootevent.js
@@ -1,5 +1,9 @@
'use strict';

var _ = require('lodash');

var UPDATE_THROTTLE = 1000;

function boot (env) {

function setupMongo (ctx, next) {
Expand Down Expand Up @@ -45,19 +49,19 @@ function boot (env) {
}

function setupListeners (ctx, next) {
function updateData ( ) {
var updateData = _.debounce(function debouncedUpdateData ( ) {
ctx.data.update(function dataUpdated () {
ctx.bus.emit('data-loaded');
});
}
}, UPDATE_THROTTLE);

ctx.bus.on('tick', function timedReloadData (tick) {
console.info('tick', tick.now);
updateData();
});

ctx.bus.on('data-received', function forceReloadData ( ) {
console.info('got data-received event, reloading now');
console.info('got data-received event, requesting reload');
updateData();
});

Expand Down
8 changes: 3 additions & 5 deletions lib/bus.js
Expand Up @@ -3,21 +3,19 @@ var Stream = require('stream');
function init (env) {
var beats = 0;
var started = new Date( );
var id;
var interval = env.HEARTBEAT || 20000;
var interval = env.HEARTBEAT || 60000;

var stream = new Stream;

function ictus ( ) {
var tick = {
return {
now: new Date( )
, type: 'heartbeat'
, sig: 'internal://' + ['heartbeat', beats ].join('/')
, beat: beats++
, interval: interval
, started: started
};
return tick;
}

function repeat ( ) {
Expand All @@ -26,7 +24,7 @@ function init (env) {

stream.readable = true;
stream.uptime = repeat;
id = setInterval(repeat, interval);
setInterval(repeat, interval);
return stream;
}
module.exports = init;
Expand Down
51 changes: 51 additions & 0 deletions tests/update-throttle.test.js
@@ -0,0 +1,51 @@
'use strict';

var _ = require('lodash');
var request = require('supertest');
require('should');

describe('Throttle', function ( ) {
var self = this;

var api = require('../lib/api/');
before(function (done) {
process.env.API_SECRET = 'this is my long pass phrase';
self.env = require('../env')();
this.wares = require('../lib/middleware/')(self.env);
self.app = require('express')();
self.app.enable('api');
require('../lib/bootevent')(self.env).boot(function booted(ctx) {
self.ctx = ctx;
self.app.use('/api', api(self.env, ctx));
done();
});
});

after(function () {
delete process.env.API_SECRET;
});

it('only update once when there are multiple posts', function (done) {

//if the data-loaded event is triggered more than once the test will fail
self.ctx.bus.on('data-loaded', function dataWasLoaded ( ) {
done()
});

function post () {
request(self.app)
.post('/api/entries/')
.set('api-secret', self.env.api_secret || '')
.send({type: 'sgv', sgv: 100, date: Date.now()})
.expect(200)
.end(function(err) {
if (err) {
done(err);
}
});
}

_.times(10, post);
});

});

0 comments on commit 99ea9fa

Please sign in to comment.