- 论坛徽章:
- 0
|
如何自动关闭空闲的sde连接?
在连接sde的应用中,如果连接的客户比较多的情况下,可能会出现sde连接占用cpu资源十分严重,以至于出现系统死机的情况。
Sde提供了TCPKEEPALIVE参数,用来侦测客户端连接情况。当TCPKEEPALIVE为True时,那么在2小时(TimeOut默认设置7200000ms,即2小时)内,如果客户端没有向sde发送请求,sde连接将自动关闭,如果TCPKEEPALIVE为false,那么当客户端没有向sde发送请求超过timeout规定时间,sde连接仍然占用。因此,如果我们想让系统自动关闭空闲的sde连接,那么我们就要把TCPKEEPALIVE设置为true。默认安装的时候TCPKEEPALIVE的值为false。
TCPKEEPALIVE参数可以通过sde命令来更改,例如:
Sdeconfig –o alter –v TCPKEEPALIVE=TRUE –u sde –p sde
改完后重启sde服务。
注意:sde直连应用的情况下TCPKEEPALIVE参数不起作用。
TCPKEEPALIVE的TimeOut时间是可以更改的,我们可以更改操作系统设置TimeOut间隔时间(5分钟-2小时)。例如:
On Microsoft Windows set KeepAliveTime to 300000.
\HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\TCPIP\Parameters\KeepAliveTime
If the key does not already exist you will need to create it.
The time specified is in milliseconds.
On Sun Solaris use the ndd command with the -set option to configure the tcp_keepalive_interval.
# ndd -set /dev/tcp tcp_keepalive_interval 300000
The time specified is in milliseconds.
On IBM AIX use the no command to set communication parameters.
no -o tcp_keepidle=591
The time specified is in halfseconds.
On HP HP-UX use
ndd -set /dev/tcp tcp_time_wait_interval 300000
The time specified is in milliseconds.
On RedHat Linux modify the following kernel parameter by editing the /etc/sysctl.conf file, and restart the network daemon (/etc/rc.d/init.d/network restart).
# Decrease the time default value for tcp_keepalive_time
tcp_keepalive_time = 1800
Linux操作系统下TCPkeepalive属性查看
Tcp是面向连接的,在实际应用中通常都需要检测连接是否还可用.如果不可用,可分为:
a. 连接的对端正常关闭.
b. 连接的对端非正常关闭,这包括对端设备掉电,程序崩溃,网络被中断等.这种情况是不能也无法通知对端的,所以连接会一直存在,浪费国家的资源.
tcp协议栈有个keepalive的属性,可以主动探测socket是否可用,不过这个属性的默认值很大.
全局设置可更改/etc/sysctl.conf,加上:
net.ipv4.tcp_keepalive_intvl = 20
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_time = 60
在程序中设置如下:
#include
#include
#include
#include
#include
int keepAlive = 1; // 开启keepalive属性
int keepIdle = 60; // 如该连接在60秒内没有任何数据往来,则进行探测
int keepInterval = 5; // 探测时发包的时间间隔为5 秒
int keepCount = 3; // 探测尝试的次数.如果第1次探测包就收到响应了,则后2次的不再发.
setsockopt(rs, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepAlive, sizeof(keepAlive));
setsockopt(rs, SOL_TCP, TCP_KEEPIDLE, (void*)&keepIdle, sizeof(keepIdle));
setsockopt(rs, SOL_TCP, TCP_KEEPINTVL, (void *)&keepInterval, sizeof(keepInterval));
setsockopt(rs, SOL_TCP, TCP_KEEPCNT, (void *)&keepCount, sizeof(keepCount));
在程序中表现为,当tcp检测到对端socket不再可用时(不能发出探测包,或探测包没有收到ACK的响应包),select会返回socket可读,并且在recv时返回-1,同时置上errno为ETIMEDOUT。
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/60332/showart_1150035.html |
|