c - 我应该在套接字生命周期内什么时候设置 TCP_QUICKACK 选项?

标签 c linux sockets tcp

我知道我为什么要使用它,但我不确定将 setsockopt 放在我的套接字代码中的什么地方。

我很清楚它可以通过套接字 api 的内部机制进行修改,但是我应该在什么时候使用 setsockopt 设置 TCP_QUICKACK 选项?

我应该在创建套接字时设置它然后在每次接收和发送之后(或之前?)?还是只接收?

我应该检查该选项是否已设置?

最佳答案

When should I set the TCP_QUICKACK option?

IETF 提供 TCP Tuning for HTTP, draft-stenberg-httpbis-tcp-03 .文档第 4.4 节解释:

Delayed ACK [RFC1122] is a mechanism enabled in most TCP stacks that causes the stack to delay sending acknowledgement packets in response to data. The ACK is delayed up until a certain threshold, or until the peer has some data to send, in which case the ACK will be sent along with that data. Depending on the traffic flow and TCP stack this delay can be as long as 500ms.

This interacts poorly with peers that have Nagle's Algorithm enabled. Because Nagle's Algorithm delays sending until either one MSS of data is provided or until an ACK is received for all sent data, delaying ACKs can force Nagle's Algorithm to buffer packets when it doesn't need to (that is, when the other peer has already processed the outstanding data).

Delayed ACKs can be useful in situations where it is reasonable to assume that a data packet will almost immediately (within 500ms) cause data to be sent in the other direction. In general in both HTTP/1.1 and HTTP/2 this is unlikely: therefore, disabling Delayed ACKs can provide an improvement in latency.

However, the TLS handshake is a clear exception to this case. For the duration of the TLS handshake it is likely to be useful to keep Delayed ACKs enabled.

Additionally, for low-latency servers that can guarantee responses to requests within 500ms, on long-running connections (such as HTTP/2), and when requests are small enough to fit within a small packet, leaving delayed ACKs turned on may provide minor performance benefits.

Effective use of switching off delayed ACKs requires extensive profiling.

在文档的后面,它提供了以下内容:

On recent Linux kernels (since Linux 2.4.4), Delayed ACKs can be disabled like this:

int one = 1;
setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));

Unlike disabling Nagle’s Algorithm, disabling Delayed ACKs on Linux is not a one-time operation: processing within the TCP stack can cause Delayed ACKs to be re-enabled. As a result, to use TCP_QUICKACK effectively requires setting and unsetting the socket option during the life of the connection.

关于c - 我应该在套接字生命周期内什么时候设置 TCP_QUICKACK 选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46587168/

相关文章:

java - Android-Unity 服务 NetworkOnMainThreadException

Java:正确关闭多线程服务器的套接字

c - n/=10 和 n=n/10 的区别

c - 使用 dup2 重定向 printf 失败

c++ - 构建一个 C++ Linux 程序。何时拆分单独的文件与单独的程序

linux - 跨服务器符号链接(symbolic link)和 Crontab

php - 关于从 php 访问影子密码文件的安全问题

java - 最大 as3 adobe JSON 字符串长度

c - 提取数字中特定位组表示的值

c - 在 EOF 处终止 scanf 并忽略字符串