Tuesday 16 May 2017

Configuring socket timeout on amqplib connect

I'm running a cluster of 2 RabbitMQ servers (could be any number) and I have implemented a failover where my app loops the list of RabbitMQs and tries to reconnect when a connection drops.

If the RabbitMQ is down which I'm trying to connect to, it takes about 60 seconds to timeout before trying to the next one, which is a very long time. Is there a way to configure the timeout or some other way to make it fail faster. This is causing an unnecessary long downtime. The heartbeat takes care of detecting a failure on an existing connection, but the problem is the initial connect attempt.

Here is my code used for connecting:

connect(callback) {
    const self = this;

    amqp.connect(rabbitInstances[rabbitInstance] + "?heartbeat=10").then(conn => {
        conn.on("error", function(err) {
            setTimeout(() => self.reconnect(callback), 5000));
            return;
        });

        conn.on("close", function() {
            setTimeout(() => self.reconnect(callback), 5000));
            return;
        });

        connection = conn;
        whenConnected(callback);
    })
    .catch(err => {
        setTimeout(() => self.reconnect(callback), 5000));
    });
}

reconnect(callback) {
    this.rabbitInstance === (rabbitInstances.length - 1) ? this.rabbitInstance = 0 : this.rabbitInstance++;
    this.connect(callback)
}



via Mikko

No comments:

Post a Comment