delayed ack和nagles算法都能减少tcp传输过程中的小数据包的问题

tcpip卷二25章中提到tcp为每个连接建立7个定时器:

1.connection established

2.restransmission

3.delayed ack(https://en.wikipedia.org/wiki/TCP_delayed_acknowledgment)

4.persist

5.keepalive

6.fin_wait_2

7.time_wait

下面是wikipedia对nagle’s算法描述的url,

https://en.wikipedia.org/wiki/Nagle%27s_algorithm

最主要的应该就是这段代码:

if there is new data to send
  if the window size >= MSS and available data is >= MSS
    send complete MSS segment now
  else
    if there is unconfirmed data still in the pipe
      enqueue data in the buffer until an acknowledge is received
    else
      send data immediately
    end if
  end if
end if

MSS(maximum segment size)

(今天才知道这是两个概念,之前以为是一个意思呢O(∩_∩)O~),

如果实际中就是小数据包很多而且实时要求很高的话,可以设置socket的tcp_nodelay选项来关闭nagles算法(nginx中就有此设置).(这里还有两个参数tcp_cork和tcp_quickack和本文有关系有时间可以研究一下这两个参数。)