【问题标题】:Setting the TCP keepalive interval on the Hiredis async context在 Hiredis 异步上下文上设置 TCP 保活间隔
【发布时间】:2017-08-10 14:38:45
【问题描述】:

我正在围绕hiredis 编写一个包装器,以便在redis 节点出现故障时通过重新连接启用发布/订阅功能。

我正在使用异步 redis API。

所以我有一个测试工具来设置发布者和订阅者。然后,该工具会关闭订阅者正在读取的从 VM。

但是,断开回调直到很久以后才被调用(当我破坏包含相应 redisAsyncContext 的 Subscription 对象时。

我认为解决这个问题的方法可能是使用 tcp keepalive。

于是我发现net.h中有一个合适的redis函数:

int redisKeepAlive(redisContext* c, int interval);

但是,以下似乎表明库中故意省略了 redisKeepAlive 函数:

$ nm libhiredis.a --demangle | grep redisKeepAlive
0000000000000030 T redisKeepAlive
                 U redisKeepAlive

$ nm libhiredis.a -u --demangle | grep redisKeepAlive
             U redisKeepAlive

当然,当我尝试使用该调用时,链接器会抱怨:

Subscription.cpp:167: undefined reference to `redisKeepAlive(redisContext*, int)'
collect2: error: ld returned 1 exit status

我运气不好 - 有没有办法在 Hiredis 异步上下文上设置 TCP 保持活动间隔?

更新 我发现了这个:

int redisEnableKeepAlive(redisContext *c);

但是在 asyncContext->c 上设置这个并调整 REDIS_KEEPALIVE_INTERVAL 似乎没有效果。

【问题讨论】:

    标签: asynchronous tcp redis hiredis


    【解决方案1】:

    我发现redisKeepAlive 的实现包含显示如何直接访问底层套接字描述符的代码:

    int redisKeepAlive(redisContext *c, int interval) {
    int val = 1;
    int fd = c->fd;
    
    if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) == -1){
        __redisSetError(c,REDIS_ERR_OTHER,strerror(errno));
        return REDIS_ERR;
    

    }

    也许这会对某人有所帮助..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-27
      • 2019-05-27
      • 1970-01-01
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多