Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection retry? #67

Closed
murbanowicz opened this issue Nov 6, 2019 · 6 comments
Closed

Connection retry? #67

murbanowicz opened this issue Nov 6, 2019 · 6 comments

Comments

@murbanowicz
Copy link

Hi,
It is a really nice module but it is missing connection retry on init or on error.
Are you interested in implementing this feature?

@WonderPanda
Copy link
Collaborator

Hey @murbanowicz I think it makes sense for this to be natively supported by the module. How do you currently implement retry/reconnection behavior in your RabbitMQ connected applications? Maybe if you share a snippet I can use that as a starting point

@murbanowicz
Copy link
Author

Hi,

I've done it in fairly simple way:

async connectToAmqp(retry = 1): Promise<amqplib.Connection> {
    if (retry > this.config.maxRetries) {
      this.logger.error('Maximum AMQP connection retries reached!');
      throw new Error('Maximum AMQP connection retries reached!');
    }
    this.logger.debug(`Connecting to AMQP. ${retry} try.`);
    try {
      const connection = await amqplib.connect(this.config.uri);
      connection.on('close', async () => {
        this.readiness = false;
        this.logger.warn('AMQP disconnected. Will try to reconnect...');
        this.connection = await this.connectToAmqp();
      });
      this.readiness = true;
      return connection;
    } catch (e) {
      this.logger.debug(`Could not connect to AMQP.`, e);
      await waitFor(this.config.reconnectDelay * retry);
      return this.connectToAmqp(retry + 1);
    }
  }

@WonderPanda
Copy link
Collaborator

Thanks for the example code, I'll try to incorporate improved retry and connection logic into the library in the coming week

@azuker
Copy link
Contributor

azuker commented Dec 2, 2019

I recommend you consider incorporating amqp-connection-manager - https://www.npmjs.com/package/amqp-connection-manager

@azuker
Copy link
Contributor

azuker commented Dec 11, 2019

I created a PR for that if you're interested - #80

@WonderPanda
Copy link
Collaborator

Connection management is now possible thanks to amqp-connection-manager and the awesome contributions from @azuker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants