Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Better handling of restarts, fixes #10

  • Loading branch information...
commit 50b0c3840dc776a998988b6bc39b08f8d2835f43 1 parent 7534b91
@kriszyp authored
Showing with 5 additions and 3 deletions.
  1. +5 −3 multi-node.js
View
8 multi-node.js
@@ -34,6 +34,7 @@ exports.listen = function(options, server){
isMaster = true;
emitter.id = "master";
var children = [],
+ childId = 1,
tcpDescriptor = netBinding.socket("tcp4");
netBinding.bind(tcpDescriptor, options.port || 80, options.host || '0.0.0.0');
netBinding.listen(tcpDescriptor, 128);
@@ -79,15 +80,15 @@ exports.listen = function(options, server){
})(child);
child.addListener("exit", function(){
// remove the dead one
- children.splice(i, 1);
+ delete children[i];
// make a new process to replace the dead one
if(options.restartChildren !== false){
- createChild(children.length);
+ createChild(childId++);
}
});
}
for(var i = 0; i < numChildren; i++){
- createChild(i);
+ createChild(childId++);
}
["SIGINT", "SIGTERM", "SIGKILL", "SIGQUIT", "SIGHUP", "exit"].forEach(function(signal){
process.addListener(signal, function(){
@@ -100,6 +101,7 @@ exports.listen = function(options, server){
});
// we use SIGHUP to restart the children
if(signal !== 'exit' && signal !== 'SIGHUP'){
+ options.restartChildren = false;
process.exit();
}
});

6 comments on commit 50b0c38

@4d47

I've considered using delete when sending 19b22a0 but I think it cannot be used because it alter elements position in children, breaking i references in the closures. Avoiding "holes" in the array would require bigger refactoring.

var a = [1,2,3,4],
    i = 2; // 4 is index 3
delete a[i]
a == [1,2,4] // 4 is now at index 2

In fact I think you lost my 3 patches when "move to root" merge (check the tabs/spaces).

@neonstalwart

the position is not lost

>a.forEach(function (c, i) { console.log(i); });
0
1
3
>
@4d47

the position is lost because i is bound when createChild is called and does not change after delete changes the length of the array.

@neonstalwart

open node's repl and try this:

> a = [1,2,3,4,5]
[ 1, 2, 3, 4, 5 ]
> delete a[2]
true
> a.length
5
> a[3]
4
> a[2] === undefined
true
>
@4d47

Ah your right I was wrong. Thank you for reply !

@neonstalwart

np - i maybe also should have mentioned that its called a "sparse array".

Please sign in to comment.
Something went wrong with that request. Please try again.