之前一直对tcp keepalive选项理解有误,

以为通过setsockopt函数设置SO_KEEPALIVE和相关参数后该socket则使用设置的keepalive相关参数

否则使用系统默认的:keepalive配置(如下)

root@xxx-KVM:/# sysctl -a | grep keep
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 9
net.ipv4.tcp_keepalive_time = 90

今天突然发现redis服务器上存在大量死链接(很多都是几个月前的连接),检查配置,未设置keepalive选项,系统默认(7200,9,75)

经过google查询出keepalive选项开启才行,系统默认的只是keepalive默认开启后配置值;

"Remember that keepalive support, even if configured in the kernel, is not the default behavior in Linux. Programs must request keepalive control for their sockets using the setsockopt interface. There are relatively few programs implementing keepalive, but you can easily add keepalive support for most of them following the instructions explained later in this document."

详细介绍请看:http://tldp.org/HOWTO/html_single/TCP-Keepalive-HOWTO/