MCPcopy Create free account
hub / github.com/F-Stack/f-stack / anetKeepAlive

Function anetKeepAlive

app/redis-6.2.6/src/anet.c:133–177  ·  view source on GitHub ↗

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. */

Source from the content-addressed store, hash-verified

131 * is only used for Linux as we are using Linux-specific APIs to set
132 * the probe send time, interval, and count. */
133int 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
179static int anetSetTcpNoDelay(char *err, int fd, int val)
180{

Callers 3

cliConnectFunction · 0.85
connKeepAliveFunction · 0.85

Calls 2

anetSetErrorFunction · 0.85
setsockoptFunction · 0.70

Tested by

no test coverage detected