lovely fixes

This commit is contained in:
javi 2011-12-07 13:06:23 +01:00
parent 5084e69aff
commit b53bcceb6d
2 changed files with 19 additions and 8 deletions

View File

@ -21,7 +21,7 @@ function VarnishClient(host, port, ready_callback) {
}
function connect() {
if(connecting) return;
if(connecting || connected ) return;
connecting = true;
log("VARNISH: connection");
ready = false;
@ -30,8 +30,8 @@ function VarnishClient(host, port, ready_callback) {
client.on('connect', function () {
log("VARNISH: connected");
connected = true;
connecting = false;
self.emit('connect');
connecting = false;
});
} else {
client.connect(port, host);
@ -155,7 +155,7 @@ function VarnishQueue(host, port) {
var ready = false;
var reconnectTimer = null;
var reconnectTries = 0;
var MAX_RECONNECT_TRIES = 50;
var MAX_RECONNECT_TRIES = 120; // 2 minutes
var client = new VarnishClient(host, port);
@ -163,15 +163,24 @@ function VarnishQueue(host, port) {
console.log.apply(console, arguments);
}
// attach a dummy callback to error event to avoid nodejs throws an exception and closes the process
self.on('error', function(e) {
log("error", e);
});
client.on('connect', function() {
clearInterval(reconnectTimer);
reconnectTries = 0;
});
client.on('ready', function() {
ready = true;
log('sending pending');
_send_pending();
reconnectTries = 0;
clearInterval(reconnectTimer);
});
function reconnect() {
ready = false;
clearInterval(reconnectTimer);
reconnectTimer = setInterval(function() {
client.connect();
@ -183,12 +192,13 @@ function VarnishQueue(host, port) {
});
clearInterval(reconnectTimer);
}
}, 500);
}, 1000);
}
client.on('close', reconnect);
client.on('error', reconnect);
function _send_pending(empty_callback) {
if(!ready) return;
var c = queue.pop();
if(!c) return;
client.run_cmd(c, function() {

View File

@ -131,11 +131,12 @@ tests['should send command'] = function() {
tests['should send commands on connect'] = function() {
// first create queue
var queue = new varnish.VarnishQueue('127.0.0.1', 1234)
for(var i = 0; i < 5; ++i) {
for(var i = 0; i < 10; ++i) {
queue.run_cmd('purge simon_is == gay');
}
// then server
var server = VarnishEmu(null, 1234)
setTimeout(function() { assert.equal(5, server.commands); }, 1000);
//wait 2 seconds because the client tries every second the reconnection
setTimeout(function() { assert.equal(10, server.commands); }, 2000);
}