Browse files

Expose a repl to help diagnose insane spike count

  • Loading branch information...
1 parent 2f8e561 commit 7c60c752e71e26248c8d2485516960779567b918 @remy remy committed Jan 13, 2014
Showing with 52 additions and 25 deletions.
  1. +1 −0 .jshintrc
  2. +49 −24 lib/spike/index.js
  3. +2 −1 package.json
View
1 .jshintrc
@@ -10,5 +10,6 @@
"node": true,
"quotmark": "single",
"undef": true,
+ "strict": true,
"unused": true
}
View
73 lib/spike/index.js
@@ -1,36 +1,51 @@
-var parse = require('url').parse,
- querystring = require('querystring').parse,
- processors = require('../processors'),
- _ = require('underscore');
+'use strict';
+var processors = require('../processors'),
+ _ = require('underscore'),
+ replify = require('replify');
var utils = {},
sessions = {},
sessionsFromUser = {},
pending = {};
var metrics = require('../metrics'),
- open = 0;
+ openSpikes = 0;
setInterval(function () {
pending = {};
}, 15 * 60 * 1000);
+replify('spike', {
+ utils: utils,
+ sessions: sessions,
+ sessionsFromUser: sessionsFromUser,
+ pending: function () {
+ return pending;
+ },
+ openSpikes: function () {
+ return openSpikes;
+ }
+});
+
+
/**
* Remove a response object from the list associated with the id passed.
*/
utils.removeConnection = function (id, res) {
- if (!sessions[id]) return;
+ if (!sessions[id]) { return; }
var responses = sessions[id].res,
index = responses.indexOf(res);
- if (index === -1) return;
+ if (index === -1) { return; }
responses.splice(index, 1);
// Update the metrics
- open -= 1;
- if (open < 0) open = 0;
- metrics.gauge('spike.open', open);
+ openSpikes -= 1;
+ if (openSpikes < 0) {
+ openSpikes = 0;
+ }
+ metrics.gauge('spike.open', openSpikes);
// Remove the object if we're not using it.
if (!responses.length) {
@@ -57,10 +72,14 @@ utils.sessionForBin = function (bin, create) {
* Turn some data (a string or object) into an event-source data & event string.
*/
utils.makeEvent = function (eventName, data) {
- if (!eventName) return '';
+ if (!eventName) { return ''; }
var str = '';
- if (typeof data !== 'string') data = JSON.stringify(data);
- if (data) str += 'data: ' + (''+data).split('\n').join('\ndata: ');
+ if (typeof data !== 'string') {
+ data = JSON.stringify(data);
+ }
+ if (data) {
+ str += 'data: ' + (''+data).split('\n').join('\ndata: ');
+ }
return 'event: ' + eventName + '\n' + str + '\n\n';
};
@@ -75,12 +94,18 @@ utils.process = function(data, settings) {
settings = settings || {};
// Grab the processor settings
var processorSettings = settings.processors;
- if (!processorSettings) return data;
+ if (!processorSettings) {
+ return data;
+ }
// Get the name of the currently used processor
var processorName = processorSettings[data.panelId];
- if (!processorName) return data;
+ if (!processorName) {
+ return data;
+ }
// Find this processor in the list of processors
- if (!processors[processorName]) return newData;
+ if (!processors[processorName]) {
+ return newData;
+ }
// And process!
var newData = _.clone(data);
newData.raw = data.content;
@@ -125,8 +150,8 @@ module.exports = {
keepalive = null,
close, closeTimer;
- open += 1;
- metrics.gauge('spike.open', open);
+ openSpikes += 1;
+ metrics.gauge('spike.open', openSpikes);
console.log('STREAM ACCEPTED', req.originalUrl, req.headers['user-agent']);
res.writeHead(200, {
@@ -193,13 +218,13 @@ module.exports = {
updateUserSpikes: function (user, newBin) {
var newKey = utils.keyForBin(newBin);
- if (!sessionsFromUser[user]) return;
+ if (!sessionsFromUser[user]) { return; }
// Update sessions for the user bins if the bin has changed
sessionsFromUser[user].forEach(function (userSession) {
var oldBinKey = userSession.key;
// Is this the same bin? If so, keep it, we're cool.
- if (oldBinKey === newKey) return;
+ if (oldBinKey === newKey) { return; }
utils.removeConnection(oldBinKey, userSession.res);
// Create/get a new session for the new bin and add the connection to the
// new session's res pool
@@ -228,7 +253,7 @@ module.exports = {
newSession = utils.sessionForBin(newBin, true),
newKey = utils.keyForBin(newBin);
- if (!oldSession) return;
+ if (!oldSession) { return; }
// Move connections from one session to another
oldSession.res.forEach(function (res) {
@@ -276,13 +301,13 @@ module.exports = {
data = null,
ping = pending[id];
- if (!session) return;
+ if (!session) { return; }
- if (!ping) return;
+ if (!ping) { return; }
// Javascript and HTML cause reloads. CSS is injected.
data = ping.javascript || ping.html || ping.css;
- if (!data) return;
+ if (!data) { return; }
// Forget this ping, it's done with now
delete pending[id];
View
3 package.json
@@ -46,7 +46,8 @@
"rsvp": "~3.0.1",
"undefsafe": "0.0.1",
"feature-gateway": "0.0.1",
- "hbs": "~2.4.0"
+ "hbs": "~2.4.0",
+ "replify": "~1.1.4"
},
"devDependencies": {
"grunt": "~0.4.1",

0 comments on commit 7c60c75

Please sign in to comment.