2010-10-16 01:05:22 +08:00
|
|
|
net = require('net');
|
|
|
|
|
|
|
|
var errors = 0, connections = 0;
|
|
|
|
|
2010-10-27 17:52:49 +08:00
|
|
|
var lastClose = 0;
|
|
|
|
|
|
|
|
function maybeConnect (s) {
|
|
|
|
var now = new Date();
|
|
|
|
if (now - lastClose > 5000) {
|
|
|
|
// Just connect immediately
|
|
|
|
connect();
|
|
|
|
} else {
|
|
|
|
// Otherwise wait a little - see if this one is connected still. Just to
|
|
|
|
// avoid spinning at 100% cpu when the server totally rejects our
|
|
|
|
// connections.
|
|
|
|
setTimeout(function () {
|
|
|
|
if (s.writable && s.readable) connect();
|
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-16 01:05:22 +08:00
|
|
|
function connect () {
|
|
|
|
process.nextTick(function () {
|
|
|
|
var s = net.Stream();
|
|
|
|
var gotConnected = false;
|
|
|
|
s.connect(9000);
|
2010-10-27 17:52:49 +08:00
|
|
|
|
2010-10-16 01:05:22 +08:00
|
|
|
s.on('connect', function () {
|
|
|
|
gotConnected = true;
|
|
|
|
connections++;
|
2010-10-27 17:52:49 +08:00
|
|
|
maybeConnect(s);
|
2010-10-16 01:05:22 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
s.on('close', function () {
|
|
|
|
if (gotConnected) connections--;
|
2010-10-27 17:52:49 +08:00
|
|
|
lastClose = new Date();
|
2010-10-16 01:05:22 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
s.on('error', function () {
|
|
|
|
errors++;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
connect();
|
|
|
|
|
|
|
|
|
|
|
|
var oldConnections, oldErrors;
|
|
|
|
|
2010-10-27 17:10:14 +08:00
|
|
|
// Try to start new connections every so often
|
|
|
|
setInterval(connect, 5000);
|
|
|
|
|
2010-10-16 01:05:22 +08:00
|
|
|
setInterval(function () {
|
|
|
|
if (oldConnections != connections) {
|
|
|
|
oldConnections = connections;
|
|
|
|
console.log("CLIENT %d connections: %d", process.pid, connections);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldErrors != errors) {
|
|
|
|
oldErrors = errors;
|
|
|
|
console.log("CLIENT %d errors: %d", process.pid, errors);
|
|
|
|
}
|
|
|
|
}, 1000);
|
|
|
|
|