我使用ubuntu(12.04)+ nodejs(v0.10.22)+ socket.io(v0.9.14)来传输消息.
有大约300个同时连接.几个小时后(大约1或2个小时,它没有立即显示),一些连接将持续处于CLOSE_WAIT或FIN_WAIT2状态.
而这些未死亡的连接随着时间线性增长.当连接数达到限制(默认1024)时,用户将难以连接套接字服务器,除非某些连接正常释放.
以下是套接字服务连接状态,运行大约3个小时.
netstat -anl | grep <PORT_OF_NODE_PROCESS> | awk '/^tcp/ {t[$NF]++}END{for(state in t){print state, t[state]} }'
FIN_WAIT2 23
LISTEN 1
CLOSE_WAIT 27
TIME_WAIT 12
ESTABLISHED 333
FIN_WAIT1 12
Run Code Online (Sandbox Code Playgroud)
使用Nodemon Package运行js文件,当更改文件的上次修改时间时,nodemon将重新启动服务,并释放所有先前的未连接连接(CLOSEWAIT或FINWAIT2)
sudo vim /etc/security/limits.conf
* soft nofile 1024
* hard nofile 2048
root soft nofile 4096
root hard nofile 8192
user1 soft nofile 2048
user1 hard nofile 2048
Run Code Online (Sandbox Code Playgroud)
尽量让连接难以达到极限.
让操作系统在短时间内自动关闭连接,但我还没试过.
我找到了一些解决问题的方法.但上述解决方案并没有真正解决状态CLOSE_WAIT …