在 Hiredis 异步上下文中设置 TCP keepalive 间隔

Setting the TCP keepalive interval on the Hiredis async context

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

我正在使用异步 redis API。

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

但是,断开连接回调直到很晚才被调用(当我破坏包含相应 redisAsyncContext 的订阅对象时。

我认为解决这个问题的方法可能是使用 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 keepalive 间隔?

更新 我发现了这个:

int redisEnableKeepAlive(redisContext *c);

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

我发现 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;

}

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