manage reconnections

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

View File

@ -12,11 +12,26 @@ 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;
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();
@ -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;
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 {