Skip to content

Instantly share code, notes, and snippets.

@jorben
Created January 21, 2019 01:35
Show Gist options
  • Save jorben/04d974d96038ceacc3a912d778cdbd73 to your computer and use it in GitHub Desktop.
Save jorben/04d974d96038ceacc3a912d778cdbd73 to your computer and use it in GitHub Desktop.
高并发应用服务大量TIME_WAIT状态优化方法
#CLOSED:无连接是活动的或正在进行
#LISTEN:服务器在等待进入呼叫
#SYN_RECV:一个连接请求已经到达,等待确认
#SYN_SENT:应用已经开始,打开一个连接
#ESTABLISHED:正常数据传输状态
#FIN_WAIT1:应用说它已经完成
#FIN_WAIT2:另一边已同意释放
#ITMED_WAIT:等待所有分组死掉
#CLOSING:两边同时尝试关闭
#TIME_WAIT:另一边已初始化一个释放
#LAST_ACK:等待所有分组死掉
# 查看连接的各状态数量
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
# 修改网络参数
vi /etc/sysctl.conf
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse=1 #让TIME_WAIT状态可以重用,这样即使TIME_WAIT占满了所有端口,也不会拒绝新的请求造成障碍 默认是0
net.ipv4.tcp_tw_recycle=1 #让TIME_WAIT尽快回收 默认0
net.ipv4.tcp_fin_timeout=30
# 让修改生效
/sbin/sysctl -p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment