There are no built-in mechanisms to limit the size of the message queue for a process.
The usual solution to this problem in erlang is to introduce a flow control protocol between the producer and the consumer. This can be as simple as the sender waiting for a continue
reply before sending the next message. You can invent more complicated flow control protocols (windowing, ...), but often send/wait-reply will do. The gen_server:call/2
protocol is a good request-response protocol and can be re-used by looking at the code for gen_server
and gen:call
- it takes care of a lot of the edge cases that are possible.
Another approach is to pull messages through the system instead of pushing them - in this case the receiver asks the sender for a message when it's ready. Though if you have external producers that you can't rate-limit or buffer sufficiently, then you may not have this option.