码迷,mamicode.com
首页 > 其他好文 > 详细

设置TCP的keepalive来进行网络联调

时间:2015-01-30 16:12:30      阅读:474      评论:0      收藏:0      [点我收藏+]

标签:

使用TCP的keepalive来检查网络错误

为了检测网络错误和信令连接问题,你可以开启TCP的keep alive 功能。
它会增加信令使用的带宽,但信令通道使用的带宽要小于它的实际带宽,增加得并不多。
而且,还可以控制它keep alive的超时时长。

问题是大多数的系统对TCP keepalive的超时时长为7200秒,约两个小时。
你可能会想要这个时间更短此,如一分钟等。
对于每个系统,调整这个参数的方式是不一样的。

在设置完所有的相关参数后,需要检测下这些设置是否生效,
就需要生成一个测试调用,然后拔出网线,看这个调用是否在超时时长后结束。

一、Linux 系统
使用 sysctl -A 命令查看所有有效的内核变量,使用 grep net.ipv4 过滤。
$ sysctl -A | grep net.ipv4

应当有下列变量
- net.ipv4.tcp_keepalive_time   - 在第一次keep alive请求发送后,不活动连接的时间                               
- net.ipv4.tcp_keepalive_probes - 在这个连接被认为是断开之前,keep alive请求被重发的次数
- net.ipv4.tcp_keepalive_intvl  - keep alive探测的时间间隔

可以使用下面的命令操作这些变量:
$ sysctl -w net.ipv4.tcp_keepalive_time=60 net.ipv4.tcp_keepalive_probes=3 net.ipv4.tcp_keepalive_intvl=10

这个命令将TCP keepalive的超时设为60秒,并有三次重发,每次间隔10秒。
因此,你的应用程序90秒(60  + 10 + 10 + 10)后检测到TCP断开

二、FreeBSD and MacOS X
For the list of available TCP settings (FreeBSD 4.8 an up and 5.4):
sysctl -A | grep net.inet.tcp

net.inet.tcp.keepidle - Amount of time, in milliseconds, that the (TCP) 
                                connection must be idle before keepalive probes (if enabled) are sent.
net.inet.tcp.keepintvl - The interval, in milliseconds, between keepalive probes sent to remote machines.
                         After TCPTV_KEEPCNT (default 8) probes are sent, with no response, 
                         the (TCP)connection is dropped.
net.inet.tcp.always_keepalive - Assume that SO_KEEPALIVE is set on all TCP connections, 
                                the kernel will periodically send a packet to the remote host to 
                                verify the connection is still up.

therefore formula to calculate maximum TCP inactive connection time is 
following:
net.inet.tcp.keepidle + (net.inet.tcp.keepintvl x 8)
the result is in milliseconds.
therefore, by setting
net.inet.tcp.keepidle = 10000
net.inet.tcp.keepintvl = 5000
net.inet.tcp.always_keepalive =1 (must be 1 always)
the system will disconnect a call when TCP connection is dead for:
10000 + (5000 x 8) = 50000 msec (50 sec)

To make system remember these settings at startup, you should add them 
to /etc/sysctl.conf file

Solaris
For the list of available TCP settings:
ndd /dev/tcp \?
Keepalive related variables:
- tcp_keepalive_interval - idle timeout
Example:
ndd -set /dev/tcp tcp_keepalive_interval 60000
?
三、Windows
Search Knowledge Base for article ID 120642:
http://support.microsoft.com/kb/120642/EN-US
Basically, you need to tweak some registry entries under
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters

Linux
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_probes = 10
net.ipv4.tcp_keepalive_intvl = 30
?
The procedures involving keepalive use three user-driven variables:
tcp_keepalive_time
the interval between the last data packet sent (simple ACKs are not considered data) 
and the first keepalive probe; after the connection is marked to need keepalive, 
this counter is not used any further
tcp_keepalive_intvl
the interval between subsequential keepalive probes, 
regardless of what the connection has exchanged in the meantime
tcp_keepalive_probes
the number of unacknowledged probes to send before considering the connection dead 
and notifying the application layer

设置TCP的keepalive来进行网络联调

标签:

原文地址:http://blog.csdn.net/fireroll/article/details/43304301

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!