Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
dvv committed Mar 15, 2011
1 parent 7fc6a91 commit 5da20ea
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
.ipc
5 changes: 3 additions & 2 deletions index.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ framing = (chunk) ->
node cluster factory, takes options: node cluster factory, takes options:
options.host - host to bind server to = * options.host - host to bind server to = '0.0.0.0'
options.port - port to bind server to = 80 options.port - port to bind server to = 80
options.connections - listener capacity = 1024 options.connections - listener capacity = 1024
Expand Down Expand Up @@ -75,6 +75,7 @@ module.exports = (options = {}) ->


# options # options
options.port ?= 80 options.port ?= 80
options.host ?= '0.0.0.0'
nworkers = options.workers or require('os').cpus().length nworkers = options.workers or require('os').cpus().length
options.ipc ?= '.ipc' options.ipc ?= '.ipc'


Expand Down Expand Up @@ -215,7 +216,7 @@ module.exports = (options = {}) ->
# #
netBinding = process.binding 'net' netBinding = process.binding 'net'
socket = netBinding.socket 'tcp' + (if netBinding.isIP(options.host) is 6 then 6 else 4) socket = netBinding.socket 'tcp' + (if netBinding.isIP(options.host) is 6 then 6 else 4)
netBinding.bind socket, options.port netBinding.bind socket, options.port, options.host
netBinding.listen socket, options.connections or 1024 netBinding.listen socket, options.connections or 1024


# #
Expand Down
23 changes: 12 additions & 11 deletions index.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ framing = function(chunk) {
node cluster factory, takes options: node cluster factory, takes options:
options.host - host to bind server to = * options.host - host to bind server to = '0.0.0.0'
options.port - port to bind server to = 80 options.port - port to bind server to = 80
options.connections - listener capacity = 1024 options.connections - listener capacity = 1024
Expand Down Expand Up @@ -67,15 +67,16 @@ options.ssl.caCerts
*/ */
module.exports = function(options) { module.exports = function(options) {
var REPL, args, cmd, comm, credentials, env, fs, ipc, k, net, netBinding, nworkers, server, socket, spawnWorker, v, watch, workers, _ref, _ref2, _ref3, _ref4; var REPL, args, cmd, comm, credentials, env, fs, ipc, k, net, netBinding, nworkers, server, socket, spawnWorker, v, watch, workers, _ref, _ref2, _ref3, _ref4, _ref5;
if (options == null) { if (options == null) {
options = {}; options = {};
} }
net = require('net'); net = require('net');
fs = require('fs'); fs = require('fs');
(_ref = options.port) != null ? _ref : options.port = 80; (_ref = options.port) != null ? _ref : options.port = 80;
(_ref2 = options.host) != null ? _ref2 : options.host = '0.0.0.0';
nworkers = options.workers || require('os').cpus().length; nworkers = options.workers || require('os').cpus().length;
(_ref2 = options.ipc) != null ? _ref2 : options.ipc = '.ipc'; (_ref3 = options.ipc) != null ? _ref3 : options.ipc = '.ipc';
if (process.env._NODE_WORKER_FOR_) { if (process.env._NODE_WORKER_FOR_) {
process.log = function() { process.log = function() {
var args; var args;
Expand Down Expand Up @@ -149,7 +150,7 @@ module.exports = function(options) {
}; };
netBinding = process.binding('net'); netBinding = process.binding('net');
socket = netBinding.socket('tcp' + (netBinding.isIP(options.host) === 6 ? 6 : 4)); socket = netBinding.socket('tcp' + (netBinding.isIP(options.host) === 6 ? 6 : 4));
netBinding.bind(socket, options.port); netBinding.bind(socket, options.port, options.host);
netBinding.listen(socket, options.connections || 1024); netBinding.listen(socket, options.connections || 1024);
if (process.getuid() === 0) { if (process.getuid() === 0) {
if (options.uid) { if (options.uid) {
Expand All @@ -165,18 +166,18 @@ module.exports = function(options) {
workers = {}; workers = {};
args = options.args || process.argv; args = options.args || process.argv;
env = {}; env = {};
_ref3 = process.env; _ref4 = process.env;
for (k in _ref3) {
if (!__hasProp.call(_ref3, k)) continue;
v = _ref3[k];
env[k] = v;
}
_ref4 = options.env || {};
for (k in _ref4) { for (k in _ref4) {
if (!__hasProp.call(_ref4, k)) continue; if (!__hasProp.call(_ref4, k)) continue;
v = _ref4[k]; v = _ref4[k];
env[k] = v; env[k] = v;
} }
_ref5 = options.env || {};
for (k in _ref5) {
if (!__hasProp.call(_ref5, k)) continue;
v = _ref5[k];
env[k] = v;
}
spawnWorker = function() { spawnWorker = function() {
var worker; var worker;
env._NODE_WORKER_FOR_ = process.pid; env._NODE_WORKER_FOR_ = process.pid;
Expand Down
19 changes: 19 additions & 0 deletions test.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,19 @@
var server = require('./')({
port: 3000,
repl: true,
workers: 4
});
if (server) { // worker process
// HTTP(S) server instance for further tuning
server.on('request', function(req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('HELLO FROM WORKER ' + process.pid);
});
// inter-workers message arrives
process.on('message', function(message){
console.log(JSON.stringify(message));
});
} else { // master process
// broadcast a message
process.publish({sos: 'to all, all, all'});
}

0 comments on commit 5da20ea

Please sign in to comment.