Commit b06b90b5 authored by antirez's avatar antirez
Browse files

Tcp keep-alive: send three probes before detectin an error.

Otherwise we end with less reliable connections because it's too easy
that a single packet gets lost.
parent 0a14a654
...@@ -100,15 +100,19 @@ int anetKeepAlive(char *err, int fd, int interval) ...@@ -100,15 +100,19 @@ int anetKeepAlive(char *err, int fd, int interval)
return ANET_ERR; return ANET_ERR;
} }
/* Send next probes after interval. */ /* Send next probes after the specified interval. Note that we set the
val = interval; * delay as interval / 3, as we send three probes before detecting
* an error (see the next setsockopt call). */
val = interval/3;
if (val == 0) val = 1;
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val)) < 0) { if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val)) < 0) {
anetSetError(err, "setsockopt TCP_KEEPINTVL: %s\n", strerror(errno)); anetSetError(err, "setsockopt TCP_KEEPINTVL: %s\n", strerror(errno));
return ANET_ERR; return ANET_ERR;
} }
/* Consider the socket in error state after just one missing ACK reply. */ /* Consider the socket in error state after three we send three ACK
val = 1; * probes without getting a reply. */
val = 3;
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val)) < 0) { if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val)) < 0) {
anetSetError(err, "setsockopt TCP_KEEPCNT: %s\n", strerror(errno)); anetSetError(err, "setsockopt TCP_KEEPCNT: %s\n", strerror(errno));
return ANET_ERR; return ANET_ERR;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment