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.
精彩评论