Comments (5)
Hi @mabels,
My understanding of the problem you describe is that when there are multiple messages on the queue, they are delivered to your consumer as fast as possible. When onMessage is an asynchronous function, amqplib does not wait for it to yield/resolve, consequently your application is being flooded with multiple messages in parallel.
The above behaviour is by design and necessary due to the amqp protocol. RabbitMQ would still deliver messages as fast as possible to amqplib even if the library did wait for each onMessage function to yield/resolve before calling the next one. This would create a real possibility of running out of memory since all of the messages would be buffered in Node's event loop. Instead the way to throttle messages is to use RabbitMQ's consumer prefetch feature in combination with explicitly acknowledged messages (the default), e.g.
await channel.prefetch(1);
await channel.consume(q.name, async (msg) => {
console.log("enter onMessage");
try {
await fetch("www.google.com");
channel.ack(msg);
console.log("success");
} catch (err) {
channel.nack(msg);
console.error(err);
}
});
from amqplib.
Hi @mabels
I think what you want can be achieved by specifying a consumer prefetch as per the channel api
from amqplib.
Hi @cressie176,
i might be my inability to explain the problem but that is not the problem. I try it with an example:
channel.consume(q.queue, async (msg) => {
console.log("enter onMessage")
await fetch("www.google.com")
console.log("the expectation is that the invokation will not called again before i reach this line")
console.log("but if consume message does not wait for the promise to resolve than")
console.log("enter onMessage will called before the fetch is resolved")
console.log("which could cause a storm of events. In js there is no way to use consume with async functions")
})
from amqplib.
Hi,
now it get it but i'm still think that this is a stretch.
Thx for the explanation.
meno
from amqplib.
Although @cressie176 's solution solved my problem (I couldn't process messages one-by-one) in a synchronous handler, because of js can't await/force sync run an async function in a sync handler, I think in a recent years devs mostly uses async-await in callbacks.
I have tried to rewrite the whole worker app with sync functions, besides it was a nightmare, I couldn't send feedbacks of processing status.
With async functions, it worked fine (I send a message in a status queue every 10 percent), with fully synced handlers I saw only the initial and the end status on the frontend.
So they wasn`t sent or sent at the end all at once.
from amqplib.
Related Issues (20)
- no error event on createChannel since V10.0.0 HOT 1
- Consider removing `readable-stream`? HOT 1
- Fanout delivery acknowledgement on channel 1 timed out HOT 6
- Set a custom header before nack-ing a message HOT 5
- No Error emitted when Queue disappears or is deleted HOT 2
- SDK connecting without errors after RabbitMQ service is shut down HOT 9
- Error emitted before handler is attached causes "Unhandled 'error' event" HOT 3
- Trailing dot in URL causes TLS failure HOT 2
- docs: channel api github page has broken top links HOT 2
- docs: confusing and inconsistent usage of terms "bork", "splode", "close" HOT 7
- global prefetch become 0 if per-consume prefetch set HOT 2
- ERR_SSL_EMS_NOT_ENABLED HOT 6
- How to determine if channel.ack() is successful? HOT 2
- CPU intensive controller prevents AMQP connection from receiving heartbeat, hence connections gets closed. HOT 2
- How to know if message is published ? HOT 6
- Aborting a Message Processing when RMQ Channel Closes HOT 8
- I'm encountering an "Unexpected close" error HOT 2
- Module not found: Can't resolve 'querystring' HOT 2
- Feature: Add support for AMQP 0.9.1 "update-secret" HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from amqplib.