Skip to content

Commit 9c7c694

Browse files
authored
Merge pull request #670 from jman-krafton/master
fix timeout code in windows
2 parents ac49287 + 8e61d57 commit 9c7c694

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

net.c

+13-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ int redisNetRead(redisContext *c, char *buf, size_t bufcap) {
6363
if ((errno == EWOULDBLOCK && !(c->flags & REDIS_BLOCK)) || (errno == EINTR)) {
6464
/* Try again later */
6565
return 0;
66+
} else if(errno == ETIMEDOUT && (c->flags & REDIS_BLOCK)) {
67+
/* especially in windows */
68+
__redisSetError(c, REDIS_ERR_TIMEOUT, "recv timeout");
69+
return -1;
6670
} else {
6771
__redisSetError(c, REDIS_ERR_IO, NULL);
6872
return -1;
@@ -310,11 +314,18 @@ int redisCheckSocketError(redisContext *c) {
310314
}
311315

312316
int redisContextSetTimeout(redisContext *c, const struct timeval tv) {
313-
if (setsockopt(c->fd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv)) == -1) {
317+
const void *to_ptr = &tv;
318+
size_t to_sz = sizeof(tv);
319+
#ifdef _WIN32
320+
DWORD timeout_msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
321+
to_ptr = &timeout_msec;
322+
to_sz = sizeof(timeout_msec);
323+
#endif
324+
if (setsockopt(c->fd,SOL_SOCKET,SO_RCVTIMEO,to_ptr,to_sz) == -1) {
314325
__redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(SO_RCVTIMEO)");
315326
return REDIS_ERR;
316327
}
317-
if (setsockopt(c->fd,SOL_SOCKET,SO_SNDTIMEO,&tv,sizeof(tv)) == -1) {
328+
if (setsockopt(c->fd,SOL_SOCKET,SO_SNDTIMEO,to_ptr,to_sz) == -1) {
318329
__redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(SO_SNDTIMEO)");
319330
return REDIS_ERR;
320331
}

0 commit comments

Comments
 (0)