# How to prevent inbound packet flood?

**URL:** <https://forum.cedalo.com/t/how-to-prevent-inbound-packet-flood/408>\
**Category:** Security\
**Created:** [December 13, 2021, 10:07pm UTC](https://forum.cedalo.com/t/how-to-prevent-inbound-packet-flood/408 "2021-12-13T22:07:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eugeny](https://avatars.discourse-cdn.com/v4/letter/e/bbce88/32.png) [@Eugeny](https://forum.cedalo.com/u/Eugeny)\
**Post date:** [December 13, 2021, 10:07pm UTC](https://forum.cedalo.com/t/how-to-prevent-inbound-packet-flood/408/1 "2021-12-13T22:07:46Z")

</div>

In my previous [question](https://forum.cedalo.com/t/publish-what-is-correct-application-of-this-call/396) 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?

---

<div class="post-metadata">

**Author:** ![roger.light](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@roger.light](https://forum.cedalo.com/u/roger.light)\
**Post date:** [December 16, 2021, 11:36am UTC](https://forum.cedalo.com/t/how-to-prevent-inbound-packet-flood/408/2 "2021-12-16T11:36:32Z")

</div>

`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?

Regards,

Roger
