Reputation: 7107
I'm working with rabbitmq
with amqplib in my node
project.
I use the queue to pass job messages between services.
One of the services consume those message, doing some process and return the result to another queue.
Sometimes the queue holds a lot of messages, and the service try to consume them all at once, hence resulting the process (service) to crash.
How can I limit the number of messages a channel / service can handle?
I thought of a custom solution, holding a global limit, but I'd rather use it as a last resort...
Upvotes: 1
Views: 1956
Reputation: 398
It's not entirely clear from your question, but it seems you want to limit how your service consumes the messages and not how many messages the queue holds.
If that understanding is correct, then what you need is prefetch_count
. More documentation: https://www.rabbitmq.com/consumer-prefetch.html
var amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', function(err, conn) {
conn.createChannel(function(err, ch) {
var q = 'task_queue';
ch.assertQueue(q, {durable: true});
ch.prefetch(1); // THIS SHOULD SOLVE YOUR PROBLEM
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q);
ch.consume(q, function(msg) {
var secs = msg.content.toString().split('.').length - 1;
console.log(" [x] Received %s", msg.content.toString());
setTimeout(function() {
console.log(" [x] Done");
ch.ack(msg);
}, secs * 1000);
});
});
});
Code source: https://www.rabbitmq.com/tutorials/tutorial-two-javascript.html
Upvotes: 4