Skip to content

Commit f0904b3

Browse files
Leonardo Alminanaedsiper
authored andcommitted
network: added tcp keepalive support
Signed-off-by: Leonardo Alminana <[email protected]>
1 parent 9a9f374 commit f0904b3

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

include/fluent-bit/flb_network.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ struct flb_net_setup {
6868
/* maximum of times a keepalive connection can be used */
6969
int keepalive_max_recycle;
7070

71+
/* enable/disable tcp keepalive */
72+
int tcp_keepalive;
73+
74+
/* interval between the last data packet sent and the first TCP keepalive probe */
75+
int tcp_keepalive_time;
76+
77+
/* the interval between TCP keepalive probes */
78+
int tcp_keepalive_interval;
79+
80+
/* number of unacknowledged probes to consider a connection dead */
81+
int tcp_keepalive_probes;
82+
7183
/* dns mode : TCP or UDP */
7284
char *dns_mode;
7385

@@ -154,6 +166,7 @@ int flb_net_socket_blocking(flb_sockfd_t fd);
154166
int flb_net_socket_nonblocking(flb_sockfd_t fd);
155167
int flb_net_socket_rcv_buffer(flb_sockfd_t fd, int rcvbuf);
156168
int flb_net_socket_tcp_fastopen(flb_sockfd_t sockfd);
169+
int flb_net_socket_tcp_keepalive(flb_sockfd_t fd, struct flb_net_setup *net);
157170

158171
/* Socket handling */
159172
flb_sockfd_t flb_net_socket_create(int family, int nonblock);

src/flb_network.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ void flb_net_setup_init(struct flb_net_setup *net)
116116
net->keepalive = FLB_TRUE;
117117
net->keepalive_idle_timeout = 30;
118118
net->keepalive_max_recycle = 0;
119+
net->tcp_keepalive = FLB_FALSE;
120+
net->tcp_keepalive_time = -1;
121+
net->tcp_keepalive_interval = -1;
122+
net->tcp_keepalive_probes = -1;
119123
net->accept_timeout = 10;
120124
net->connect_timeout = 10;
121125
net->io_timeout = 0; /* Infinite time */
@@ -298,6 +302,51 @@ int flb_net_socket_tcp_fastopen(flb_sockfd_t fd)
298302
return setsockopt(fd, SOL_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen));
299303
}
300304

305+
306+
/*
307+
* Enable TCP keepalive
308+
*/
309+
int flb_net_socket_tcp_keepalive(flb_sockfd_t fd, struct flb_net_setup *net)
310+
{
311+
int interval;
312+
int enabled;
313+
int probes;
314+
int time;
315+
int ret;
316+
317+
enabled = 1;
318+
319+
time = net->tcp_keepalive_time;
320+
probes = net->tcp_keepalive_probes;
321+
interval = net->tcp_keepalive_interval;
322+
323+
ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
324+
(const void *) &enabled, sizeof(enabled));
325+
326+
if (ret == 0 && time >= 0) {
327+
ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
328+
(const void *) &time, sizeof(time));
329+
}
330+
331+
if (ret == 0 && interval >= 0) {
332+
ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
333+
(const void *) &interval, sizeof(interval));
334+
}
335+
336+
if (ret == 0 && probes >= 0) {
337+
ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
338+
(const void *) &probes, sizeof(probes));
339+
}
340+
341+
if (ret != 0) {
342+
flb_error("[net] failed to configure TCP keepalive for connection #%i", fd);
343+
344+
ret = -1;
345+
}
346+
347+
return ret;
348+
}
349+
301350
flb_sockfd_t flb_net_socket_create(int family, int nonblock)
302351
{
303352
flb_sockfd_t fd;

0 commit comments

Comments
 (0)