Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hosts & Accessible Server Instance (and tests) #50

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ var server = require('http').createServer(function(req, res) {
res.end('worker: ' + cluster.worker.id);
});

if (!sticky.listen(server, 3000)) {
var options = {env:{"foo":"Some Environment Variables (optional)"}};

if (!sticky.listen(server, 3000, options)) {
// Master code
server.once('listening', function() {
console.log('server started on 3000 port');
Expand All @@ -29,6 +31,30 @@ if (!sticky.listen(server, 3000)) {
```
Simple

## Options
The options object is optional and can be passed in as a third parameter to sticky.listen.
### env
Object: An object containing key/value environment variables that each worker will receive.
### host
String: A string value that the server will bind to as its host.
### workers
Integer: The number of workers to bring up. Defaults to the number of available cores.
### returnInstance
Boolean: Affects what is returned by the sticky.listen function. Defaults to `false`.
When false: return `false` for tha master cluster process, and `true` to workers.
When true: return the server instance to the master cluster process, and `false` to workers.
```
var serverInstance = sticky.listen(server, 3000, {returnInstance: true});
if(serverInstance !== false){
//Master code
server.once('listening', function() {
var address = serverInstance.address();
console.log("Server listening on " + address.address + ":" + address.port");
});
} else {
//Worker code
}
```
## Reasoning

Socket.io is doing multiple requests to perform handshake and establish
Expand Down
6 changes: 3 additions & 3 deletions lib/sticky/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ function listen(server, port, options) {
var workerCount = options.workers || os.cpus().length;

var master = new Master(workerCount, options.env);
master.listen(port);
master.listen(port, options.host);
master.once('listening', function() {
server.emit('listening');
});
return false;
return options.returnInstance === true ? master : false;
}

// Override close callback to gracefully close server
Expand All @@ -39,6 +39,6 @@ function listen(server, port, options) {
socket.server = server;
server.emit('connection', socket);
});
return true;
return options.returnInstance !== true;
}
exports.listen = listen;
5 changes: 5 additions & 0 deletions test/100-connections-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ var http = require('http');

var PORT = 13845;

setTimeout(function(){
console.error("Test time exceeded");
process.exit(1);
}, 5000);

var server = http.createServer(function(req, res) {
res.writeHead(200, {
'X-Sticky': process.pid
Expand Down
30 changes: 19 additions & 11 deletions test/fork-options-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,25 @@ var http = require('http');

var PORT = 13845;

var server = http.createServer(function(req, res) {
res.end('hello world');
});
setTimeout(function(){
console.error("Test time exceeded");
process.exit(1);
}, 5000);

if (sticky.listen(server, PORT, { workers: 1, env: { ohai: 23 } })) {
process.send(process.env.ohai);
return;
}
var server = http.createServer(function (req, res) {
res.end('hello world');
});

if (sticky.listen(server, PORT, {workers: 1, env: {ohai: 23}})) {
setTimeout(function () {
process.send(process.env.ohai);
}, 500);
} else {
// Master
cluster.workers[Object.keys(cluster.workers)[0]].on('message', function(msg) {
assert.equal(msg, '23');
process.exit(0);
});
setTimeout(function () {
cluster.workers[Object.keys(cluster.workers)[0]].on('message', function (msg) {
assert.equal(msg, '23');
process.exit(0);
});
}, 250);
}
5 changes: 5 additions & 0 deletions test/graceful-close-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ var http = require('http');

var PORT = 13845;

setTimeout(function(){
console.error("Test time exceeded");
process.exit(1);
}, 5000);

var done = true;

var server = http.createServer(function(req, res) {
Expand Down
35 changes: 35 additions & 0 deletions test/host-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var sticky = require('../');

var assert = require('assert');
var cluster = require('cluster');
var http = require('http');

var PORT = 13845;

setTimeout(function(){
console.error("Test time exceeded");
process.exit(1);
}, 5000);
var completed = 0;
var runHost = function(host, expectation){

var server = http.createServer(function (req, res) {
res.end('hello world');
});

var serverInstance = sticky.listen(server, PORT, {returnInstance: true, host: host, workers: 1, env: {ohai: 23}});
if(serverInstance === true){
process.exit(0);
}

server.once('listening', function() {
assert.equal(serverInstance.address().port, PORT);
assert.equal(serverInstance.address().address, expectation);
completed++;
if(completed == 2){
process.exit(0);
}
});
};
runHost(undefined, "::");
runHost("localhost", "127.0.0.1");
26 changes: 26 additions & 0 deletions test/master-object-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var sticky = require('../');

var assert = require('assert');
var cluster = require('cluster');
var http = require('http');

var PORT = 13845;

setTimeout(function(){
console.error("Test time exceeded");
process.exit(1);
}, 5000);

var server = http.createServer(function (req, res) {
res.end('hello world');
});

var serverInstance = sticky.listen(server, PORT, {returnInstance: true, workers: 1, env: {ohai: 23}});
if(serverInstance === false){
process.exit(0);
}

server.once('listening', function() {
assert.equal(serverInstance.address().port, PORT);
process.exit(0);
});