赞
踩
KeepAlive可以简单理解为一种状态保持或重用机制,比如当一条连接建立后,我们不想它立刻被关闭,如果实现了KeepAlive机制,就可以通过它来实现连接的保持。
简单的说就是当你打电话但是没话讲,又不能挂的时候,每隔固定周期喊一声“喂你还在吗”的机制。
Implementors MAY include "keep-alives" in their TCP implementations, although this practice is not universally accepted. If keep-alives are included, the application MUST be able to turn them on or off for each TCP connection, and they MUST default to off.
另外有一个特殊情况就是,当某应用进程关闭后,如果还有该进程相关的TCP连接,一般来说操作系统会自动关闭这些连接
Linux中KeepAlive相关的配置可以通过如下方式查看
- cat /proc/sys/net/ipv4/tcp_keepalive_time
- cat /proc/sys/net/ipv4/tcp_keepalive_intvl
- cat /proc/sys/net/ipv4/tcp_keepalive_probes
在Linux中我们可以通过修改内核参数 /etc/sysctl.conf 的全局配置:
- net.ipv4.tcp_keepalive_time=7200
- net.ipv4.tcp_keepalive_intvl=75
- net.ipv4.tcp_keepalive_probes=9
添加上面的配置后输入 sysctl -p 使其生效,你可以使用 sysctl -a | grep keepalive 命令来查看当前的默认配置
如果应用中已经设置SO_KEEPALIVE,程序不用重启,内核直接生效
ss -aoen|grep ESTAB
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。