Skip to content

Commit

Permalink
Merge branch 'feature/connection-stats'
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Mar 23, 2011
2 parents 1b696cb + 4d34365 commit 9fec2b7
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 43 deletions.
3 changes: 1 addition & 2 deletions examples/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ cluster.repl.define('echo', function(master, sock, msg){
}, 'echo the given message');

// $ telnet localhots 8888

cluster(server)
.set('workers', 4)
.set('socket path', '/tmp')
.use(cluster.logger('logs'))
.use(cluster.stats())
.use(cluster.stats({ connections: true }))
.use(cluster.repl(8888, '127.0.0.1'))
.use(cluster.debug())
.listen(3000);
6 changes: 5 additions & 1 deletion lib/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,11 @@ Master.prototype.has = function(option){
Master.prototype.use = function(plugin){
if (this.environmentMatches) {
this.plugins.push(plugin);
if (!this.isWorker) plugin(this);
if (this.isWorker) {
plugin.enableInWorker && plugin(this);
} else {
plugin(this);
}
}
return this;
};
Expand Down
132 changes: 92 additions & 40 deletions lib/plugins/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,50 +23,90 @@ try {
}

/**
* Enable stat tracking.
* Enable stat tracking with the given `options`.
*
* Options:
*
* - `connections` enable connection statistics
*
* @param {Object} options
* @return {Function}
* @api public
*/

module.exports = function(){
return function(master){
master.stats = {
start: new Date
, restarts: 0
, workersSpawned: 0
, workersKilled: 0
};

// 0.4.x
if (os) {
master.stats.totalmem = os.totalmem();
master.stats.freemem = os.freemem();
module.exports = function(options){
options = options || {};
stats.enableInWorker = options.connections;

function stats(master){
var server = master.server;

// worker stats
if (master.isWorker) {
server.on('connection', function(sock){
master.call('reportStats', 'connection');
sock.on('close', function(){
master.call('reportStats', 'disconnection');
});
});
// master stats
} else {
master.stats = {
start: new Date
, restarts: 0
, workersSpawned: 0
, workersKilled: 0
};

// 0.4.x
if (os) {
master.stats.totalmem = os.totalmem();
master.stats.freemem = os.freemem();
}

// worker stats
master.reportStats = function(worker, type){
switch (type) {
case 'connection':
worker.stats.connectionsTotal++;
worker.stats.connectionsActive++;
break;
case 'disconnection':
worker.stats.connectionsActive--;
break;
}
};

// total workers spawned
master.on('worker', function(worker){
++master.stats.workersSpawned;
worker.stats = {
start: new Date
, connectionsTotal: 0
, connectionsActive: 0
};
});

// total worker deaths
master.on('worker killed', function(worker){
++master.stats.workersKilled;
});

// restarting
master.on('restarting', function(data){
++master.stats.restarts;
data.stats = master.stats;
});

// restart
master.on('restart', function(data){
master.stats = data.stats;
master.stats.start = new Date(master.stats.start);
});
}

// total workers spawned
master.on('worker', function(worker){
++master.stats.workersSpawned;
worker.stats = { start: new Date };
});

// total worker deaths
master.on('worker killed', function(worker){
++master.stats.workersKilled;
});

// restarting
master.on('restarting', function(data){
++master.stats.restarts;
data.stats = master.stats;
});

// restart
master.on('restart', function(data){
master.stats = data.stats;
master.stats.start = new Date(master.stats.start);
});
}

return stats;
};

/**
Expand Down Expand Up @@ -105,9 +145,21 @@ repl.define('stats', function(master, sock){
// worker stats
sock.title('Workers');
master.children.forEach(function(worker){
sock.row(
'uptime #' + worker.id
, utils.formatDateRange(new Date, worker.stats.start));
var stats = '';

// uptime
stats += utils.formatDateRange(new Date, worker.stats.start);

// connections
if (worker.stats.connectionsTotal) {
stats += ' ('
+ worker.stats.connectionsActive
+ '/'
+ worker.stats.connectionsTotal
+ ')';
}

sock.row(worker.id, stats);
});
sock.write('\n');
}, 'Display server statistics');

0 comments on commit 9fec2b7

Please sign in to comment.