美文网首页
redis.conf详解之timeout

redis.conf详解之timeout

作者: 小易哥学呀学 | 来源:发表于2021-11-21 21:49 被阅读0次

    用法

    单位是

    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
    

    相关文章

      网友评论

          本文标题:redis.conf详解之timeout

          本文链接:https://www.haomeiwen.com/subject/ilovtrtx.html