Skip to content

Commit 131712c

Browse files
committed
support set keepalive interval parameter
1 parent a910ca2 commit 131712c

File tree

6 files changed

+61
-6
lines changed

6 files changed

+61
-6
lines changed

src/nc_proxy.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ proxy_accept(struct context *ctx, struct conn *p)
359359
}
360360

361361
if (pool->tcpkeepalive) {
362-
status = nc_set_tcpkeepalive(c->sd);
362+
status = nc_set_tcpkeepalive(c->sd, CLIENT_KEEP_INTERVAL);
363363
if (status < 0) {
364364
log_warn("set tcpkeepalive on c %d from p %d failed, ignored: %s",
365365
c->sd, p->sd, strerror(errno));

src/nc_proxy.h

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#include <nc_core.h>
2222

23+
#define CLIENT_KEEP_INTERVAL 72
24+
2325
void proxy_ref(struct conn *conn, void *owner);
2426
void proxy_unref(struct conn *conn);
2527
void proxy_close(struct context *ctx, struct conn *conn);

src/nc_sentinel.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ sentinel_connect(struct context *ctx, struct server *sentinel)
6666
}
6767

6868
/* set keepalive opt on sentinel socket to detect socket dead */
69-
status = nc_set_tcpkeepalive(conn->sd);
69+
status = nc_set_tcpkeepalive(conn->sd, SENTINEL_KEEP_INTERVAL);
7070
if (status < 0) {
7171
log_error("set keepalive on s %d for sentienl server failed: %s",
7272
conn->sd, strerror(errno));

src/nc_sentinel.h

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include <nc_core.h>
55

6+
#define SENTINEL_KEEP_INTERVAL 30
7+
68
#define INFO_SENTINEL "info sentinel\r\n"
79
#define SUB_SWITCH_REDIRECT "subscribe +switch-master +redirect-to-master\r\n"
810

src/nc_util.c

+54-3
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,61 @@ nc_set_linger(int sd, int timeout)
110110
}
111111

112112
int
113-
nc_set_tcpkeepalive(int sd)
113+
nc_set_tcpkeepalive(int sd, int interval)
114114
{
115-
int val = 1;
116-
return setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
115+
int val, status;
116+
socklen_t len;
117+
118+
val = 1;
119+
len = sizeof(val);
120+
status = setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, &val, len);
121+
if (status == -1)
122+
{
123+
log_error("setsockopt SO_KEEPALIVE to %d on %d failed: %s",
124+
val, sd, strerror(errno));
125+
return status;
126+
}
127+
128+
#ifdef __linux__
129+
/* Default settings are more or less garbage, with the keepalive time
130+
* set to 7200 by default on Linux. Modify settings to make the feature
131+
* actually useful. */
132+
133+
/* Send first probe after interval. */
134+
val = interval;
135+
status = setsockopt(sd, IPPROTO_TCP, TCP_KEEPIDLE, &val, len);
136+
if (status < 0) {
137+
log_error("setsockopt TCP_KEEPIDLE to %d on %d failed: %s",
138+
val, sd, strerror(errno));
139+
return status;
140+
}
141+
142+
/* Send next probes after the specified interval. Note that we set the
143+
* delay as interval / 3, as we send three probes before detecting
144+
* an error (see the next setsockopt call). */
145+
val = interval/3;
146+
if (val == 0) val = 1;
147+
status = setsockopt(sd, IPPROTO_TCP, TCP_KEEPINTVL, &val, len);
148+
if (status < 0) {
149+
log_error("setsockopt TCP_KEEPINTVL to %d on %d failed: %s",
150+
val, sd, strerror(errno));
151+
return status;
152+
}
153+
154+
/* Consider the socket in error state after three we send three ACK
155+
* probes without getting a reply. */
156+
val = 3;
157+
status = setsockopt(sd, IPPROTO_TCP, TCP_KEEPCNT, &val, len);
158+
if (status < 0) {
159+
log_error("setsockopt TCP_KEEPCNT to %d on %d failed: %s",
160+
val, sd, strerror(errno));
161+
return status;
162+
}
163+
#else
164+
((void) interval); /* Avoid unused var warning for non Linux systems. */
165+
#endif
166+
167+
return 0;
117168
}
118169

119170
int

src/nc_util.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ int nc_set_tcpnodelay(int sd);
8585
int nc_set_linger(int sd, int timeout);
8686
int nc_set_sndbuf(int sd, int size);
8787
int nc_set_rcvbuf(int sd, int size);
88-
int nc_set_tcpkeepalive(int sd);
88+
int nc_set_tcpkeepalive(int sd, int interval);
8989
int nc_get_soerror(int sd);
9090
int nc_get_sndbuf(int sd);
9191
int nc_get_rcvbuf(int sd);

0 commit comments

Comments
 (0)