PNP: non-blocking IO

When short write happens in non-blocking IO

  • Save remaining data in some buffer
    • Never call write() when buffer is not empty, it reorders data
    • Alternatively, always send from buffer
  • Start watching POLLOUT event
    • Meanwhile, any write() should append the buffer instead
  • When POLLOUT is ready, write from buffer
    • consume buffer
  • If buffer becomes empty, stop watching POLLOUT event
    • Otherwise, it end up with a busy loop

使用非阻塞IO进行写数据时,没有写完要放到一个发送缓冲区中,并注册POLLOUT事件,当事件发生时,将发送缓冲区中的数据发送出去。当发送缓冲区为空时,关闭POLLOUT事件。

What if sink is slow

发送方和接收方速度不匹配时该怎么办?
比如一个proxy服务器。

  • The common pitfall in non-blocking IO
    • Avoid memory exhaustion or dropping messages

高水位回调和低水位回调,当接收缓冲区达到高水位时,停止接收。当接收缓冲区中的数据量慢慢减少到低水位时,在开始接收。

Level-trigger and edge trigger

  • select and poll are level-trigger
  • epoll stands for edge-poll, works in both LT and ET mode
  • No up-to-date benchmarks show which is faster
  • Edge trigger works best for writing and accepting
  • Level trigger works best for reading data
  • Current linux kernel doesn’t support mix LT/ET for on socket
    • muduo uses level trigger

IO-multiplexing in a multithreaded program

one loop per thread

你可能感兴趣的:(pnp)