3

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)
}
Mikko
  • 1,877
  • 1
  • 25
  • 37
  • Did you manage to work this out? – karsep5 Nov 16 '17 at 16:24
  • No I haven't found a solution. If I figure out something I will post it here. However I'm currently working on something else so this is not something I'm actively looking at. – Mikko Nov 16 '17 at 16:50

1 Answers1

4

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.

blackcatweb
  • 1,003
  • 1
  • 10
  • 11
  • Great! Haven't been working with amqplib for a couple of years, but it does seem that this has been added to the socket options already a while ago: [link to PR](https://github.com/squaremo/amqp.node/pull/118/commits/85bde27673d4cb14f36b16da13ce6a241c47068e). It's just missing from the documentation. Thanks for figuring this out! – Mikko Aug 22 '19 at 17:49
  • 1
    Anyone coming back to this answer, the timeout is only on the handshake timeout, not on a connect timeout. – Liam Sorsby Sep 09 '21 at 22:26