开发者

how to reduce the waiting time for connection?

simple script for connect server:

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

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

    if (reply != NULL) {
      开发者_StackOverflow社区  printf("Connection error: %s", reply->reply);
        exit(1);
    }

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

if the server is available - everything is normal. If not - there is a long pause. How to reduce the waiting time to 2 seconds for example ?


i don't know much about redis. but I assume, the redisConnect internally is basically also just calling connect() on an blocking fd.

so try setting the timeout beforehand using setsockopt:

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

this sets the send timout to 2 sec, for the receive, you basically do the same.

cheers,


You'll need to modify the hiredis library, and the anetTcpGenericConnect function to make connect timeout aware. There's an example here of how to do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜