Last active
January 2, 2016 13:09
-
-
Save sam-github/8308489 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cluster = require('cluster'); | |
var net = require('net'); | |
// listen every second | |
if(process.env.WORKER || cluster.isWorker) { | |
process.on('internalMessage', function(m) { | |
console.log('worker receives', m); | |
}); | |
listen(); | |
function listen() { | |
var s = net.createServer(); | |
setTimeout(function() { | |
s.listen(3000) | |
.on('listening', function() { | |
console.log('listening, close and try again'); | |
s.close(listen); | |
}) | |
.on('error', function(er) { | |
console.log('error:', er); | |
listen(); | |
}); | |
}, 5000); | |
} | |
} else { | |
cluster.fork().process.on('internalMessage', function(m) { | |
console.log('master receives', m); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do:
nc -l 3000
or `node -e 'require("net").createServer().listen(3000)'even if code is changed so worker exits, and a new worker is fork()ed to try to listen again, it still never succeeds
the protocol/address/port key in the master server handles table is never cleared on listen failure, so once listen fails, listen will never succeed again.