Skip to content

Instantly share code, notes, and snippets.

@sam-github
Last active December 28, 2015 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sam-github/7574579 to your computer and use it in GitHub Desktop.
Save sam-github/7574579 to your computer and use it in GitHub Desktop.
demonstrate how cluster in node v0.10 doesn't close server handles when no workers need them
var cluster = require('cluster')
var net = require('net');
var spawn = require('child_process').spawn;
if(cluster.isMaster) {
cluster.fork()
cluster.on('exit', function() {
//cluster.disconnect(); // if .disconnect() is called, it closes the serverHandlers
setTimeout(function() {
spawn('nc', ['-v', '-l', '4444'], {stdio: 'inherit'})
}, 1000)
})
} else {
var port = 4444;
var server = net.createServer()
.listen(port, function() {
console.log('pid %d listen on:', process.pid, port);
process.nextTick(function() {
process.disconnect()
})
})
.on('error', function(er) {
console.log('listen error:', er)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment