When should I use TCP_NODELAY and when TCP_CORK?

2019-01-08 06:17发布

I understood that both of them disable Nagle's algorithm.

When should/ shouldn't I use each one of them?

4条回答
smile是对你的礼貌
2楼-- · 2019-01-08 06:29

It's an optimisation, so like any optimisation:

  1. Do not use it
  2. Wait until performance becomes a problem, then having determined that socket latency is definitely the cause of it, and testing proves that this will definitely fix it, AND this is the easiest way of fixing it, do it.

Basically the aim is to avoid having to send out several frames where a single frame can be used, with sendfile() and its friends.

So for example, in a web server, you send the headers followed by the file contents, the headers will be assembled in-memory, the file will then be sent directly by the kernel. TCP_CORK allows you to have the headers and the beginning of the file sent in a single frame, even with TCP_NODELAY, which would otherwise cause the first chunk to be sent out immediately.

查看更多
倾城 Initia
3楼-- · 2019-01-08 06:36

TCP_NODELAY

used to disable nagle's algorithm to improve tcp/ip networks and decrease the number of packets by waiting until an acknowledgment of previous sent data is received to send the accumulated packets.

//From the tcp(7) manual:

TCP_CORK (or TCP_NOPUSH in FreeBSD)

If set, don't send out partial frames. All queued partial frames are sent when the option is cleared again. This is useful for prepending headers before calling sendfile(2), or for throughput optimization. As currently implemented, there is a 200 millisecond ceiling on the time for which output is corked by TCP_CORK. If this ceiling is reached, then queued data is automatically transmitted. This option can be combined with TCP_NODELAY only since Linux 2.5.71. This option should not be used in code intended to be portable.

查看更多
虎瘦雄心在
4楼-- · 2019-01-08 06:41

First of all not both of them disables Nagle's algorithm.

Nagle's algorithm is for reducing more number of small network packets in wire. The algorithm is: if data is smaller than a limit (usually MSS), wait until receiving ACK for previously sent packets and in the mean time accumulate data from user. Then send the accumulated data.

if [ data > MSS ]
    send(data)
else
    wait until ACK for previously sent data and accumulate data in send buffer (data)
    And after receiving the ACK send(data)

This will help in applications like telnet. However, waiting for the ACK may increase latency when sending streaming data. Additionally, if the receiver implements the 'delayed ACK policy', it will cause a temporary deadlock situation. In such cases, disabling Nagle's algorithm is a better option.

So TCP_NODELAY is used for disabling Nagle's algorithm.

TCP_CORK aggressively accumulates data. If TCP_CORK is enabled in a socket, it will not send data until the buffer fills to a fixed limit. Similar to Nagle's algorithm, it also accumulates data from user but until the buffer fills to a fixed limit not until receiving ACK. This will be useful while sending multiple blocks of data. But you have to be more careful while using TCP_CORK.

Until 2.6 kernel, both of these options are mutually exclusive. But in later kernel, both of them can exist together. In such case, TCP_CORK will be given more preference.

Ref:

查看更多
你好瞎i
5楼-- · 2019-01-08 06:42

TCP_CORK is the opposite of TCP_NODELAY. The former forces packet-accumulation delay; the latter disables it.

查看更多
登录 后发表回答