manage reconnections

This commit is contained in:
javi 2011-12-05 18:28:37 +01:00
parent 421a0a6efb
commit e7784c9a65

View File

@ -12,12 +12,27 @@ function VarnishClient(host, port, ready_callback) {
var ready = false;
var cmd_callback = null;
var client = null;
var connected = false;
var connecting = false;
function connect() {
if(connecting) return;
connecting = true;
console.log("VARNISH: connection");
ready = false;
client = net.createConnection(port, host);
if(!client) {
client = net.createConnection(port, host);
client.on('connect', function () {
console.log("VARNISH: connected");
connected = true;
connecting = false;
});
} else {
client.connect(port, host);
}
}
connect();
client.on('data', function(data) {
@ -45,19 +60,27 @@ function VarnishClient(host, port, ready_callback) {
});
client.on('close', function() {
console.log("[INFO] closed varnish connection");
self.close();
connected = false;
connecting = false;
});
// sends the command to the server
function _send(cmd, callback) {
cmd_callback = callback;
client.write(cmd + '\n');
if(connected) {
client.write(cmd + '\n');
} else {
connect();
}
}
// run command if there is no peding response
// fist param of the callback are the error, null
// if all went ok
this.run_cmd = function(cmd, callback) {
if(!connected) connect();
if(!cmd_callback) {
_send(cmd, callback);
} else {