用法
单位是秒
timeout 3
用途
在timeout时间内如果没有数据交互,redis侧将关闭连接。
没有数据交互
:redis客户端不向服务端发送任何数据。
注意事项
1.0
代表永不断开(在macOS测试不受内核保活定时器影响
)。
2.tcp/ip
连接、unix socket
连接均受timeout
影响。
2.timeout
在源码中对应server.maxidletime
3.源码以及一些注释:
// 路径:timeout.c line:55
/* Check for timeouts. Returns non-zero if the client was terminated.
* The function gets the current time in milliseconds as argument since
* it gets called multiple times in a loop, so calling gettimeofday() for
* each iteration would be costly without any actual gain. */
int clientsCronHandleTimeout(client *c, mstime_t now_ms) {
time_t now = now_ms/1000;
if (server.maxidletime &&
/* This handles the idle clients connection timeout if set. */
!(c->flags & CLIENT_SLAVE) && /* No timeout for slaves and monitors */
!(c->flags & CLIENT_MASTER) && /* No timeout for masters */
!(c->flags & CLIENT_BLOCKED) && /* No timeout for BLPOP */
!(c->flags & CLIENT_PUBSUB) && /* No timeout for Pub/Sub clients */
(now - c->lastinteraction > server.maxidletime)) // 当前时间-最后互动时间 大于 超时时间
{
serverLog(LL_VERBOSE,"Closing idle client");
freeClient(c); // 释放连接
return 1;
} else if (c->flags & CLIENT_BLOCKED) {
/* Cluster: handle unblock & redirect of clients blocked
* into keys no longer served by this server. */
if (server.cluster_enabled) {
if (clusterRedirectBlockedClientIfNeeded(c))
unblockClient(c);
}
}
return 0;
}
macOS修改/查看保活时器参数
查看
sysctl -a | grep "net.inet.tcp.keep"
修改
sudo sysctl -w net.inet.tcp.keepidle=7200000
sudo sysctl -w net.inet.tcp.keepintvl=75000
sudo sysctl -w net.inet.tcp.keepinit=75000
sudo sysctl -w net.inet.tcp.keepcnt=8
linux查看保活定时器参数
cat /proc/sys/net/ipv4/tcp_keepalive_time
cat /proc/sys/net/ipv4/tcp_keepalive_intvl
cat /proc/sys/net/ipv4/tcp_keepalive_probes
原生注释
# Close the connection after a client is idle for N seconds (0 to disable)
#timeout 3
网友评论