@@ -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+
301350flb_sockfd_t flb_net_socket_create (int family , int nonblock )
302351{
303352 flb_sockfd_t fd ;
0 commit comments