Skip to content

Commit 840b491

Browse files
#40. Net statistics support
1 parent 5916c27 commit 840b491

10 files changed

+106
-5
lines changed

README.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
[![Build Status](https://travis-ci.org/godaddy/node-cluster-service.png)](https://travis-ci.org/godaddy/node-cluster-service) [![NPM version](https://badge.fury.io/js/cluster-service.png)](http://badge.fury.io/js/cluster-service) [![Dependency Status](https://gemnasium.com/godaddy/node-cluster-service.png)](https://gemnasium.com/godaddy/node-cluster-service) [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/godaddy/node-cluster-service/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
44

55

6+
67
## Install
78

89
npm install cluster-service
910

1011
https://npmjs.org/package/cluster-service
1112

1213

14+
1315
## About
1416

1517
The short answer:
@@ -35,6 +37,7 @@ Stability:
3537
with either a deprecation schedule, or documented as a breaking change if necessary.
3638

3739

40+
3841
## Getting Started
3942

4043
Turning your single process node app/service into a fault-resilient multi-process service with all of the bells and whistles has never been easier!
@@ -99,6 +102,7 @@ You can even pipe raw JSON, which can be processed by the caller:
99102
Check out ***Cluster Commands*** for more details.
100103

101104

105+
102106
## Start Options
103107

104108
When initializing your service, you have a number of options available:
@@ -151,6 +155,8 @@ Or within your node app:
151155
extension. If the module exposes the "id" property, that will be the name of the command,
152156
otherwise the filename (minus the extension) will be used as the name of the command. If relative
153157
paths are provided, they will be resolved from process.cwd().
158+
* `servers` - One (object) or more (array of objects) `Net` objects to listen for ***Net Stats***.
159+
154160

155161

156162
## Console & REST API
@@ -193,6 +199,7 @@ Commands may be disabled by overriding them.
193199
* `upgrade all|pid workerPath { [cwd] [timeout:60] }` - Gracefully upgrade service, one worker at a time. (continuous deployment support).
194200
* `workers` - Returns list of active worker processes.
195201
* `health` - Returns health of service. Can be overidden by service to expose app-specific data.
202+
* `info` - Returns summary of process & workers.
196203

197204

198205

@@ -256,7 +263,7 @@ Additionally, a worker may optionally perform cleanup tasks prior to exit, via:
256263
## Access Control
257264

258265
Commands may be granted "inproc" (high risk), "local" (low risk), or "remote" (no risk). Setting
259-
access control at compile time can be done within the command, like so:
266+
access control can be done within the command, like so:
260267

261268
```javascript
262269
// exit.js
@@ -291,6 +298,24 @@ different location. This capability is still a work in progress, but initial tes
291298
* Upgrade reports success
292299

293300

301+
302+
## Net Stats
303+
304+
As of v0.8, you may optionally collect network statistics within cservice. To enable this feature,
305+
you must provide all Net object servers (tcp, http, https, etc) in one of two ways:
306+
307+
var server = require("http").createServer(onRequest);
308+
require("cluster-service").netStats(server); // listen to net stats
309+
310+
Or you can provide the net server objects via the ```servers``` option:
311+
312+
var server = require("http").createServer(onRequest);
313+
require("cluster-service").workerReady({ servers: [server] }); // listen to net stats
314+
315+
Net statistics summary may be found via the ```info``` command, and individual process
316+
net statistics may be found via the ```workers``` command.
317+
318+
294319

295320
## Tests & Code Coverage
296321

cluster-service.js

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ if (cluster.isMaster === true) {
5252
}
5353

5454
exports.start = require("./lib/start");
55+
exports.netStats = require("./lib/net-stats");
5556

5657
if (
5758
cluster.isWorker === true

lib/commands/info.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var cservice = require("../../cluster-service");
2+
3+
module.exports = function(evt, cb, cmd) {
4+
cservice.trigger("workers", function(err, results) {
5+
if (err) {
6+
cb(err);
7+
return;
8+
}
9+
10+
var workers = results.workers;
11+
var summary = {
12+
workers: { active: workers.length },
13+
memory: { rss: 0, heapTotal: 0, heapUsed: 0 },
14+
net: { connections: 0, connectionsOpen: 0, requests: 0 }
15+
};
16+
17+
for (var i = 0; i < workers.length; i++) {
18+
var w = workers[i];
19+
var p = w.process;
20+
summary.memory.rss += p.memory.rss;
21+
summary.memory.heapTotal += p.memory.heapTotal;
22+
summary.memory.heapUsed += p.memory.heapUsed;
23+
summary.net.connections += p.net.connections;
24+
summary.net.connectionsOpen += p.net.connectionsOpen;
25+
summary.net.requests += p.net.requests;
26+
}
27+
28+
cb(null, summary);
29+
}, "simple");
30+
};
31+
32+
module.exports.more = function(cb) {
33+
cb(null, {
34+
command: "info",
35+
info: "Returns summary of process & workers."
36+
});
37+
};
38+
39+
module.exports.control = function() {
40+
return "remote"; // consistent with "workers" command,
41+
// but may be locked down in the future
42+
};

lib/commands/workers.js

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ function getProcessDetails(worker) {
4747
cb("processDetails not returned");
4848
return; // end
4949
}
50-
5150
worker.processDetails = msg.processDetails;
5251
cb(null, worker);
5352
});

lib/defaults.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = exports = {
44
firstTime: true,
55
events: {},
66
workers: {},
7+
stats: { net: { requests: 0, connections: 0, connectionsOpen: 0 } },
78
state: 0, // 0-not running, 1-starting, 2-running
89
isAttached: false, // attached to CLI over REST
910
workerReady: undefined,

lib/net-stats.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var cservice = require("../cluster-service");
2+
3+
module.exports = exports = netStats;
4+
5+
function netStats(server) {
6+
server.on("connection", function(connection) {
7+
cservice.locals.stats.net.connections++;
8+
cservice.locals.stats.net.connectionsOpen++;
9+
connection.on("close", function() {
10+
cservice.locals.stats.net.connectionsOpen--;
11+
});
12+
});
13+
14+
server.on("request", function() {
15+
cservice.locals.stats.net.requests++;
16+
});
17+
}

lib/worker-ready.js

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var cservice = require("../cluster-service"),
22
cluster = require("cluster"),
3+
util = require("util"),
34
onWorkerStop = null;
45

56
module.exports = exports = workerReady;
@@ -23,6 +24,15 @@ function workerReady(options, forceIsWorker) {
2324

2425
options = options || {};
2526

27+
if (options.servers) {
28+
if (util.isArray(options.servers) === false) {
29+
options.servers = [options.servers];
30+
}
31+
for (var i = 0; i < options.servers.length; i++) {
32+
require("./net-stats")(options.servers[i]);
33+
}
34+
}
35+
2636
onWorkerStop = options.onWorkerStop;
2737

2838
process.on("message", onMessageFromMaster);

lib/workers.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var cluster = require("cluster");
1+
var cservice = require("../cluster-service"),
2+
cluster = require("cluster");
23

34
exports.get = get;
45
exports.monitor = monitor;
@@ -47,7 +48,8 @@ function monitor() {
4748
process.send({
4849
cservice: "processDetails",
4950
processDetails: {
50-
memoryUsage: process.memoryUsage(),
51+
net: cservice.locals.stats.net,
52+
memory: process.memoryUsage(),
5153
title: process.title,
5254
uptime: process.uptime(),
5355
hrtime: process.hrtime()

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cluster-service",
3-
"version": "0.7.0",
3+
"version": "0.8.0",
44
"author": {
55
"name": "Aaron Silvas",
66
"email": "[email protected]"

vs/node-cluster-service.njsproj

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
<PropertyGroup Condition="'$(Configuration)' == 'Debug'" />
1717
<PropertyGroup Condition="'$(Configuration)' == 'Release'" />
1818
<ItemGroup>
19+
<Compile Include="lib\commands\info.js" />
20+
<Compile Include="lib\commands\workerExit.js" />
21+
<Compile Include="lib\commands\workerStart.js" />
22+
<Compile Include="lib\net-stats.js" />
1923
<Compile Include="test\bin.js" />
2024
<Compile Include="test\commands2\custom-name.js" />
2125
<Compile Include="test\commands\custom.js" />

0 commit comments

Comments
 (0)