How to prevent inbound packet flood?

In my previous question I was asking how to deal with outbound packet flood.

This question is about how to deal with inbound messages in the application.

For example, application is able to process only one message at a time, and is not ready to accept more messages until some specific time.

What is the best way for implementation in this case?

Right now I am performing mosquitto_loop() manually. I see I can do mosquitto_loop_read() when ready to receive message, and mosquitto_loop_write() when ready to send message (e.g. after publish). But I am unsure because receiving messages, as well as publishing messages, involve both “read operations” and “write operations” (e.g. for publish send data and then receive PUBACK). What “read operations” means in mosquitto_loop_read() - perform logical read from the topic (which involves sending and receiving packets), or perform physical receiving only?

Next, how do I control how many messages I receive using mosquitto_loop_read() or mosquitto_loop()? Is it something about max_packets argument? Why it is “currently unused” as there’s clear use case for it?

mosquitto_loop_read() will attempt to do a network read and then process the data that it receives. You should only call it if you have used epoll or similar to tell you when there is data available to be read. Likewise mosquitto_loop_write() will attempt to write any pending data to the network. You should only call it if you have used epoll and data can be written. You should only ask epoll to check for the ability to write when mosquitto_want_write() returns true, otherwise there is no data available to write.

The documentation around max_packets appears to be incorrect - it is used. If you set to 1, then only a single read attempt will be made. This means that at most one single MQTT packet will be read, but it may also be zero complete packets read. This covers the whole set of MQTT packets, not just publishes, so your read may correspond to a PINGRESP at any point, for example - which you won’t see anything about.

Does that help?



1 Like