Two notification styles
A readiness interface like epoll can report events in two modes. The choice changes how your loop must be written, and getting it wrong causes connections to stall mysteriously.
Level triggered
Level triggered mode reports a descriptor as ready for as long as the condition holds. If a socket has unread bytes, every epoll wait keeps reporting it ready until you read all the data. This is forgiving. You can read a little, return to the loop, and you will simply be told again that more remains.
Edge triggered
Edge triggered mode reports only the transition from not ready to ready. You get one notification when new data arrives, and nothing more until even newer data shows up. If you read only part of the buffer and stop, the leftover bytes will not trigger another event and the connection appears frozen.
The drain rule
- In edge triggered mode you must read in a loop until you get EWOULDBLOCK
- That guarantees you have consumed everything the single edge announced
- Edge mode means fewer wakeups and less syscall overhead at high load
- Level mode is easier to write but can wake you repeatedly for the same data
Key idea
Level triggered keeps reporting while data remains, while edge triggered fires once per transition and forces you to drain the socket until EWOULDBLOCK.