Search code examples
node.jsrabbitmqamqpnode-amqplib

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 instance 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)
}

Solution

  • I read the source code for amqplib and saw the second argument to connect accepts an object that contains ordinary socket options. I used that to impose and verify a 2-second timeout as follows:

    const amqp = require('amqplib');
    
    const connection = await amqp.connect('amqp://localhost', {
      timeout: 2000,
      servername: 'localhost',
    });
    

    I am using version 0.5.3 of amqplib. The Github URL is here: https://github.com/squaremo/amqp.node.