用法
tcp-backlog 511
用途
在linux系統(tǒng)中控制tcp三次握手已完成連接隊列的長度。
在高并發(fā)系統(tǒng)中,你需要設(shè)置一個較高的tcp-backlog來避免客戶端連接速度慢的問題(三次握手的速度)。
注意事項
1.已完成連接隊列的長度也與操作系統(tǒng)中somaxconn有關(guān),取二者最小min(tcp-backlog,somaxconn)
// linux查看已完成連接隊列的長度
$ /proc/sys/net/core/somaxconn
//mac查看已完成連接隊列的長度
$ sysctl -a|grep somaxconn
2.已完成連接隊列又與半連接隊列長度有關(guān)
// linux查看半連接隊列長度
$ /proc/sys/net/ipv4/tcp_max_syn_backlog
//mac查看半連接隊列長度
$ sysctl -a|grep backlog
3.簡要介紹下半連接 與已完成連接
半連接:服務(wù)端收到客戶端syn后,將連接放入半連接隊列。如果半連接隊列已滿會丟棄,客戶端報錯connection time out。
已完成連接:服務(wù)端收到客戶端的ack后,從半連接隊列中拿出連接放入已完成連接隊列。如果已完成連接隊列已經(jīng)滿則無法放入,客戶端報錯read timeout 或者 connection reset by peer
原生注釋
# TCP listen() backlog.
#
# In high requests-per-second environments you need a high backlog in order
# to avoid slow clients connection issues. Note that the Linux kernel
# will silently truncate it to the value of /proc/sys/net/core/somaxconn so
# make sure to raise both the value of somaxconn and tcp_max_syn_backlog
# in order to get the desired effect.
tcp-backlog 1