Set TCP keep alive option to detect dead peers. The interval option * is only used for Linux as we are using Linux-specific APIs to set * the probe send time, interval, and count. */
| 131 | * is only used for Linux as we are using Linux-specific APIs to set |
| 132 | * the probe send time, interval, and count. */ |
| 133 | int anetKeepAlive(char *err, int fd, int interval) |
| 134 | { |
| 135 | int val = 1; |
| 136 | |
| 137 | if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) == -1) |
| 138 | { |
| 139 | anetSetError(err, "setsockopt SO_KEEPALIVE: %s", strerror(errno)); |
| 140 | return ANET_ERR; |
| 141 | } |
| 142 | |
| 143 | #ifdef __linux__ |
| 144 | /* Default settings are more or less garbage, with the keepalive time |
| 145 | * set to 7200 by default on Linux. Modify settings to make the feature |
| 146 | * actually useful. */ |
| 147 | |
| 148 | /* Send first probe after interval. */ |
| 149 | val = interval; |
| 150 | if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val)) < 0) { |
| 151 | anetSetError(err, "setsockopt TCP_KEEPIDLE: %s\n", strerror(errno)); |
| 152 | return ANET_ERR; |
| 153 | } |
| 154 | |
| 155 | /* Send next probes after the specified interval. Note that we set the |
| 156 | * delay as interval / 3, as we send three probes before detecting |
| 157 | * an error (see the next setsockopt call). */ |
| 158 | val = interval/3; |
| 159 | if (val == 0) val = 1; |
| 160 | if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val)) < 0) { |
| 161 | anetSetError(err, "setsockopt TCP_KEEPINTVL: %s\n", strerror(errno)); |
| 162 | return ANET_ERR; |
| 163 | } |
| 164 | |
| 165 | /* Consider the socket in error state after three we send three ACK |
| 166 | * probes without getting a reply. */ |
| 167 | val = 3; |
| 168 | if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val)) < 0) { |
| 169 | anetSetError(err, "setsockopt TCP_KEEPCNT: %s\n", strerror(errno)); |
| 170 | return ANET_ERR; |
| 171 | } |
| 172 | #else |
| 173 | ((void) interval); /* Avoid unused var warning for non Linux systems. */ |
| 174 | #endif |
| 175 | |
| 176 | return ANET_OK; |
| 177 | } |
| 178 | |
| 179 | static int anetSetTcpNoDelay(char *err, int fd, int val) |
| 180 | { |
no test coverage detected