【TCP】TCP Timers

发布时间 2023-08-30 14:59:27作者: Angel挤一挤
 

TCP协议可靠性保证,依赖的三个定时器

 

1.Retransmission Timer

To retransmit lost segments, TCP uses retransmission timeout (RTO). When TCP sends a segment the timer starts and stops when the acknowledgment is received. If the timer expires timeout occurs and the segment is retransmitted.
 
解释:send数据遇到网络抖动时,不能及时收到ack,会触发tcp重传,若网络在配置的重传次数(Linux默认15次)消耗完之前恢复,send调用可以成功,比如:短暂拔掉网线/专线,又重新接上,发送数据的业务层可能只感知到响应时间变长。若网络异常中断,send调用方在没有配置tcp socketTimeout的情况下,只能在重传次数耗尽后(Linux默认约15分钟),才能发现tcp中断。
 
线上相关系统参数配置:
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_retries1 = 3
net.ipv4.tcp_retries2 = 15

 

超时重传:
A keepalive timer is used to prevent a long idle connection between two TCPs. If a client opens a TCP connection to a server transfers some data and becomes silent the client will crash. In this case, the connection remains open forever. So a keepalive timer is used. Each time the server hears from a client, it resets this timer. The time-out is usually 2 hours. If the server does not hear from the client after 2 hours, it sends a probe segment. If there is no response after 10 probes, each of which is 75 s apart, it assumes that the client is down and terminates the connection.
 
解释:长时间没有数据传输的tcp连接,有可能另外一端已中断,keep alive机制可以发现这类异常连接,并做清理。清理规则:tcp空闲超过net.ipv4.tcp_keepalive_time,开始发起tcp探针检测,检测间隔
net.ipv4.tcp_keepalive_intvl,当检测次数达到 net.ipv4.tcp_keepalive_probes,目标端依然无响应
 
线上相关系统参数配置:
net.ipv4.tcp_keepalive_time = 1800 # seconds
net.ipv4.tcp_keepalive_probes = 10
net.ipv4.tcp_keepalive_intvl = 30

 

 

3.Time Wait Timer

This timer is used during tcp connection termination. The timer starts after sending the last Ack for 2nd FIN and closing the connection.

After a TCP connection is closed, it is possible for datagrams that are still making their way through the network to attempt to access the closed port. The quiet timer is intended to prevent the just-closed port from reopening again quickly and receiving these last datagrams.
The quiet timer is usually set to twice the maximum segment lifetime (the same value as the Time-To-Live field in an IP header), ensuring that all segments still heading for the port have been discarded.
 
 
Reference:
https://www.geeksforgeeks.org/tcp-timers/
https://pracucci.com/linux-tcp-rto-min-max-and-tcp-retries2.html