npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

amqplib-binary-retry

v1.0.2

Published

Retry failed attempts to consume a message, with increasing delays between each attempt. For the delay use binary delay message arhitecture.

Downloads

106

Readme

amqplib-binary-retry

Retry failed attempts to consume a message, with increasing delays between each attempt. For the delay use binary delay message arhitecture.

The problem with a simple wait exchange and Queue Pattern.

The common pattern for the delay is that you set up a wait exchange and queue where you send messages for retries. You set a message TTL and set the dead letter exchange of your wait queue to your principal exchange. So if you set the TTL on your message to 5 minutes, then the message sits in the wait queue for 5 minutes then gets dead lettered back to your applications exchange to be consumed again.

The problem is because messages are only removed from the head of the queue. You cannot use a single wait queue for any back-off strategy. If you have a message with a TTL of 10 minutes at the head of the queue and a message with a TTL of 1 minute behind it, the second message will wait for ten minutes.

How this lib solves that

Create multiple wait queues and set a message TTL on the queues themselves. Bind those queues to the cooresponding exchanges on the each layer.

By default we create 16 delay layers. Each layer delays the message for the 2^<level> seconds. Then we set custom headers on the each retryed message to know in which layers the messages should be delayed. Last delay layer forwards the message to the 'ready' queue. Then that message is republished with original headers to the original consumer queue.

Instalation

npm install amqplib-binary-retry

Example

(async () => {
    const amqplib = require('amqplib');
    const { retryer } = require('amqplib-binary-retry');

    const CONSUMER_QUEUE = "example-service";

    const connection = await amqplib.connect('amqp://localhost:5672');
    const channel = await connection.createChannel();

    // Create the client queue
    await channel.assertQueue(CONSUMER_QUEUE, { durable: false, autoDelete: true });

    // Define a message handler
    const handler = function (msg) {
        // no need to 'ack' or 'nack' messages
        // messages that generate an exception (or a rejected Promise) will be retried
        console.log(msg);
    };

    // Define the optional delayFunction
    // const delayFunction = function (attempt) {
    //     // After three retries fail the message
    //     if (attempt > 3) {
    //         return -1;
    //     }
    //
    //     // Delay for 5 seconds
    //     return 5000;
    // };

    // Use retryer as a consumer
    // For more configuration options check the 'Options' section
    channel.consume(CONSUMER_QUEUE, retryer({
        channel,
        consumerQueue: CONSUMER_QUEUE,
        handler,
        // delayFunction,
    }));
})();

Options

| Parameter Name | Required | Default | Description | |-|-|-|-| | channel | X | | Amqplib channel | | consumerQueue | X | | Name of the queue that holds the amqp messages that need to be processed. | | handler | X | | Callback to be invoked with each message. | | failureQueue | | <consumerQueue>.failure | Name of the queue that holds the amqp messages that could not be processed in spite of the retries. | | delayFunction | | Math.pow(2, <# of attempts>) | Delay in milliseconds between retries. The function accepts the number of retry attempts. | | exchangePrefixName | | delay_exchange_<level> | Name of the exchanges used on the delay levels. | | queuePrefixName | | delay_queue_<level> | Name of the queues used on the delay levels. | | delayLevels | | 16 | Number of delay layers used. Max avaliable seconds for the delay is 2^<levels> |

Testing

Setup the required services.

docker-compose up

Transpile the typescript source.

npm run build

Run tests.

npm run test

License

MIT