【问题标题】:how to reduce the waiting time for connection?如何减少连接等待时间?
【发布时间】:2011-04-18 13:13:08
【问题描述】:

连接服务器的简单脚本:

#include "hiredis.h"
int main(void) {
    int fd;

    unsigned int j;
    redisReply *reply;
    reply = redisConnect(&fd, "test.com", 6379);

    if (reply != NULL) {
        printf("Connection error: %s", reply->reply);
        exit(1);
    }

    reply = redisCommand(fd,"PING");
    printf("PONG: %s\n", reply->reply);
    freeReplyObject(reply);
}

如果服务器可用 - 一切正常。如果没有 - 有很长的停顿。例如,如何将等待时间减少到 2 秒?

【问题讨论】:

    标签: c redis


    【解决方案1】:

    我对redis了解不多。但我认为,redisConnect 内部基本上也只是在阻塞 fd 上调用 connect()。

    因此请尝试使用setsockopt 预先设置超时:

    struct timeval timeout;
    timeout.tv_usec = 0;
    timeout.tv_sec = 2;
    setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (void *)&timeout, sizeof(timeout));
    

    这会将发送超时设置为 2 秒,对于接收,您基本上也是这样做的。

    干杯,

    【讨论】:

      【解决方案2】:

      您需要修改hiredis 库和anetTcpGenericConnect 函数以使连接超时感知。有一个示例here 说明如何操作。

      【讨论】:

        猜你喜欢
        • 2015-04-19
        • 1970-01-01
        • 1970-01-01
        • 2018-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-08
        相关资源
        最近更新 更多