当队列太大时可以在 nodejs 中使用 amqplib 丢弃 rabbitmq 消息吗?

Can one drop rabbitmq messages when queues are too large using amqplib in nodejs?

我在 nodejs 应用程序中使用 amqplib。前端有一些繁重的处理,导致接收 rabbitmq 消息时出现一些延迟。当使用 rabbitmqctl list_queues 监控我的队列时,等待处理的消息数量从未停止攀升。

有没有办法设置我的频道,以便在队列中有给定数量的消息等待时丢弃消息?

以下是我设置频道的方式:

amqp.connect('amqp://localhost')
    .then(conn => {
        return conn.createChannel();
    })
    .then(channel => {
        channel.assertQueue(q, {durable: false, autoDelete: true});
        channel.prefetch(1);
        channel.bindQueue(q.queue, topic, 'myroutingkey');

        return channel.consume(q, (msg) => {
            mycallback(channel, msg);
        });
    })
    .then(() => {})
    .catch(err => {});

感谢 Jørgen 指向 rabbitmq 文档的指针,我在 amqplib 文档中找到了等效项,并且我可以通过将 maxLength: 1 添加到我的队列设置来强制删除行为

channel.assertQueue(q, {durable: false, autoDelete: true, maxLength: 1});