Skip to content

Commit

Permalink
Tcp keep-alive: send three probes before detectin an error.
Browse files Browse the repository at this point in the history
Otherwise we end with less reliable connections because it's too easy
that a single packet gets lost.
  • Loading branch information
antirez committed Feb 11, 2013
1 parent 0fe052e commit 2d89c53
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/anet.c
Expand Up @@ -100,15 +100,19 @@ int anetKeepAlive(char *err, int fd, int interval)
return ANET_ERR;
}

/* Send next probes after interval. */
val = interval;
/* Send next probes after the specified interval. Note that we set the
* 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) {
anetSetError(err, "setsockopt TCP_KEEPINTVL: %s\n", strerror(errno));
return ANET_ERR;
}

/* Consider the socket in error state after just one missing ACK reply. */
val = 1;
/* Consider the socket in error state after three we send three ACK
* probes without getting a reply. */
val = 3;
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val)) < 0) {
anetSetError(err, "setsockopt TCP_KEEPCNT: %s\n", strerror(errno));
return ANET_ERR;
Expand Down

0 comments on commit 2d89c53

Please sign in to comment.