Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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)
                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, 2017 at 16:50

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.

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. It's just missing from the documentation. Thanks for figuring this out! – Mikko Aug 22, 2019 at 17:49 Anyone coming back to this answer, the timeout is only on the handshake timeout, not on a connect timeout. – Liam Sorsby Sep 9, 2021 at 22:26

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.