[erlang-questions] inets {read_packets,N} option

Jesper Louis Andersen <>
Sun Jan 8 16:31:49 CET 2017


Packet delivery happens roughly as follows:

1. The network card gets the packet.
2. The network card stores the packet in a kernel buffer.
3. The Erlang VM reads packets out of the kernel buffer and into its own
memory space. This opens up the kernel buffer for more UDP packets to
arrive.
4. The Erlang VM sends a message to a mailbox of a process with the data.

Rules:

* If the kernel buffer is full, new arrivals are dropped on the floor.
* The kernel buffer is set separately, often with an operating system
default.
* The VM won't flood a process unless it has {active, true} set.

{read_packets, K} controls what happens in 3. in the above. If set at 5,
once the kernel tells the VM there are data available to read, the VM will
read out 5 packets. It won't read more packets until a new packet arrives
at the socket in which case it will read up to 5 packets more. Note that if
the arrival rate is larger than the consumption rate then packets will
eventually gets dropped on the floor. With UDP, your responsibility is to
handle this situation.

{active, N} controls how many packets can pre-delivered into the mailbox
with no signs of progress from the process itself. It corresponds to a
limiter at 4. in the above. You have situations when N < K, N > K and N ==
K to think about here. In those situations, you may end up with lost
packets if the system goes over the capacity it is able to systematically
handle.

The kernel tracks stats for how many packets it has thrown away. I don't
know if the Erlang VM does somewhere (it should, provided that it throws
data on the floor).

On Sat, Jan 7, 2017 at 10:39 PM Frank Muller <>
wrote:

> Hi Alex
>
> Still didn't get it. In your example 100 UDP packets arrive so the
> socket's ready for reading. Then, N=5 are read out of 100.
>
> Why you said 5 will be read until new one arrives?
> There's still 95 ready for reading right away after delivering the first
> 5. I'm right?
>
> Thank you.
> /Frank
>
> Le sam. 7 janv. 2017 à 22:17, Alex S. <> a écrit :
>
> {read_packets, X} refers to number of UDP packets that are read whenever
> the socket is notified "ready". So if 100 UDP packets arrive at the same
> time, only 5 will be read until a new one arrives. There isn't really a way
> to know how many dgrams are waiting on a socket, so that's an anti-flood
> option.
> {active, X} refers to number of UDP packets that are immediately sent to
> Erlang process instead of being read into an internal buffer.
>
> 2017-01-07 14:04 GMT+03:00 Frank Muller <>:
>
> Hi guys,
>
> Can someone explain me the semantic behind {read_packets, Integer} for UDP:
> http://erlang.org/doc/man/inet.html
>
> I understand the associated doc, but what i can’t get is how this option
> affects me if i set it for example with:
> 1. {read_packets, 20} + {active, once}
> 2. {read_packets, 20} + {active, 100}
>
> How many packets my process will receive in each case?
>
> And are these packets send as multiple messages one packet at a time, or
> as one message representing a list of N packets?
>
> Thank you
> /Frank
>
>
>
> _______________________________________________
>
>
> erlang-questions mailing list
>
>
> 
>
>
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
>
>
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170108/84d330d8/attachment.html>


More information about the erlang-questions mailing list