关于TIME_WAIT数量过多,超过系统限制的问题

160 views
Skip to first unread message

ben

unread,
Feb 24, 2012, 9:15:44 PM2/24/12
to dev4s...@googlegroups.com
在应用中,需要频繁的建立关闭连接,造成出现很多TIME_WAIT,不知大家有什么好的办法来减少它?

Ju Chen

unread,
Feb 25, 2012, 9:54:27 AM2/25/12
to dev4s...@googlegroups.com
可以用连接池啊?
出现大量的TIME_WAIT影响系统了?不影响的话就不用管吧。
----------

Best Regards!
Chen Ju


welcome to my blog:http://blog.rushcj.com

-EOF-



2012/2/25 ben <lizp...@gmail.com>
在应用中,需要频繁的建立关闭连接,造成出现很多TIME_WAIT,不知大家有什么好的办法来减少它?

--
高性能服务器研发与运营
 http://groups.google.com/group/dev4server

ben

unread,
Feb 25, 2012, 8:32:53 PM2/25/12
to dev4s...@googlegroups.com
linux下,对TIME_WAIT出现的数量是有限制的吧

Ju Chen <samm...@gmail.com>编写:

>--
>高性能服务器研发与运营
> http://groups.google.com/group/dev4server

zhihui lin

unread,
Feb 26, 2012, 12:45:04 AM2/26/12
to dev4s...@googlegroups.com
可以让对端关闭连接

--
高性能服务器研发与运营
 http://groups.google.com/group/dev4server

dong.yajun

unread,
Feb 26, 2012, 8:04:54 AM2/26/12
to dev4s...@googlegroups.com
是的,可以通过某种方式让对端关闭连接。

Ric Dong 
Newegg Ecommerce, MIS department 


阿杜

unread,
Feb 26, 2012, 8:08:12 AM2/26/12
to dev4s...@googlegroups.com
UNP里有详细的解释和解决方法。
知易行难

邢红瑞

unread,
Feb 26, 2012, 9:26:31 PM2/26/12
to dev4s...@googlegroups.com
减少2MSL时间
对于Windows,修改注册表,在HKEY_LOCAL_MACHINE SYSTEMCurrentControlSetServices TcpipParameters上添加一个DWORD类型的值TcpTimedWaitDelay,最小为30。
bsd 不建议使用
禁用LINGER c使用setsockopt,类型为SOL_SOCKET,SO_DONTLINGER,java直接s.setSoLinger(true,0);
一般可以 SO_REUSEADDR,client.setReuseAddress(true);
求职 java架构师或高级工程师

Ju Chen

unread,
Feb 26, 2012, 10:57:35 PM2/26/12
to dev4s...@googlegroups.com
设置 SO_REUSEADDR不能减少 TIME_WAIT,这个opt的意义是socket可以绑定到这个TIME_WAIT上的端口。

要搞掉TIME_WAIT办法很多,问题是为什么要这么做,如果无所谓,那么设置SO_LINGER(LINGER时间后,对方来访问直接返回RST)或者降低TIME_WAIT的值都是可以的。

真的要搞,可以尝试下RFC1323 (http://tools.ietf.org/html/rfc1323#page-29) 的办法,使用timestamp的办法,改下tcp配置即可。不过这种方式在nat后面的机器会有问题。


其实,真的觉得这是个设计问题,从设计上想办法减少频繁的主动链接关闭,而不要采用tricky的办法。


----------

Best Regards!
Chen Ju


welcome to my blog:http://blog.rushcj.com

-EOF-



2012/2/27 邢红瑞 <csdn....@gmail.com>



--
求职 java架构师或高级工程师

--
高性能服务器研发与运营
http://groups.google.com/group/dev4server

龚辟愚

unread,
Nov 9, 2012, 12:53:04 AM11/9/12
to dev4s...@googlegroups.com
其实可以通过设置超时探测来解决的。我的解决方案的实现代码:
/**
* @brief   
* @param   bool isKeepAlive 是否进行保持在线探测
* @param   int keepalivetime 检测TCP 是否连接正常的心跳帧探测时间
* @param   int keepaliveinterval 如果没有得到确认,连续发送询问探测的时间间隔.
* @return  bool
*/
bool ClientSocket::keepAlive(bool isKeepAlive,int keepalivetime,int keepaliveinterval)
{
char c= (char)isKeepAlive;
int nRet = ::setsockopt(_s, SOL_SOCKET, SO_KEEPALIVE, (char*)&c, sizeof(c));

if(isKeepAlive){
struct tcp_keepalive ka = {TRUE,keepalivetime,keepaliveinterval},ko={0};
unsigned long ulBytesReturn = 0;

nRet = WSAIoctl(_s, SIO_KEEPALIVE_VALS, &ka, sizeof(tcp_keepalive), &ko, sizeof(tcp_keepalive), &ulBytesReturn, NULL, NULL);
return nRet != SOCKET_ERROR;
}
return  true;
}


--
高性能服务器研发与运营
http://groups.google.com/group/dev4server

Reply all
Reply to author
Forward
0 new messages