diff options
author | Pieter Noordhuis <pcnoordhuis@gmail.com> | 2013-05-01 09:23:17 -0700 |
---|---|---|
committer | Pieter Noordhuis <pcnoordhuis@gmail.com> | 2013-05-01 09:23:17 -0700 |
commit | 91f47a1c198f78206ef02e791e0c5b5dfbeebe3a (patch) | |
tree | 0efcd2d651a075e0849cb45fdbcae4b613c297f5 /net.c | |
parent | 63ce20dd15f83c6ff2bdab1bf3dcd1866e826c71 (diff) | |
parent | bb3c4c17c45c89d980e2f4f59f59e55d13c77334 (diff) |
Merge branch 'keepalive'
Also see pull request #161.
Diffstat (limited to 'net.c')
-rw-r--r-- | net.c | 39 |
1 files changed, 39 insertions, 0 deletions
@@ -113,6 +113,45 @@ static int redisSetBlocking(redisContext *c, int fd, int blocking) { return REDIS_OK; } +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; + } + +#ifdef _OSX + val = interval; + if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &val, sizeof(val)) < 0) { + __redisSetError(c,REDIS_ERR_OTHER,strerror(errno)); + return REDIS_ERR; + } +#else + val = interval; + if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val)) < 0) { + __redisSetError(c,REDIS_ERR_OTHER,strerror(errno)); + return REDIS_ERR; + } + + val = interval/3; + if (val == 0) val = 1; + if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val)) < 0) { + __redisSetError(c,REDIS_ERR_OTHER,strerror(errno)); + return REDIS_ERR; + } + + val = 3; + if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val)) < 0) { + __redisSetError(c,REDIS_ERR_OTHER,strerror(errno)); + return REDIS_ERR; + } +#endif + + return REDIS_OK; +} + static int redisSetTcpNoDelay(redisContext *c, int fd) { int yes = 1; if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) == -1) { |