From 499a0e19cb23c25c09f835e62967c2238b0e4fc1 Mon Sep 17 00:00:00 2001 From: ValdikSS Date: Sat, 13 Nov 2021 21:31:49 +0000 Subject: [PATCH] Add TCP keep alive support in Linux --- transport/internet/sockopt_linux.go | 16 ++++++++++++++-- transport/internet/system_dialer.go | 6 +++++- transport/internet/system_listener.go | 4 ++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/transport/internet/sockopt_linux.go b/transport/internet/sockopt_linux.go index 0ec2995ae..c6bcf0e70 100644 --- a/transport/internet/sockopt_linux.go +++ b/transport/internet/sockopt_linux.go @@ -59,10 +59,16 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf } } - if config.TcpKeepAliveInterval != 0 { + if config.TcpKeepAliveInterval > 0 { if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil { return newError("failed to set TCP_KEEPINTVL", err) } + if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPIDLE, int(config.TcpKeepAliveIdle)); err != nil { + return newError("failed to set TCP_KEEPIDLE", err) + } + if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil { + return newError("failed to set SO_KEEPALIVE", err) + } } } @@ -93,10 +99,16 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) } } - if config.TcpKeepAliveInterval != 0 { + if config.TcpKeepAliveInterval > 0 { if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil { return newError("failed to set TCP_KEEPINTVL", err) } + if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPIDLE, int(config.TcpKeepAliveIdle)); err != nil { + return newError("failed to set TCP_KEEPIDLE", err) + } + if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil { + return newError("failed to set SO_KEEPALIVE", err) + } } } diff --git a/transport/internet/system_dialer.go b/transport/internet/system_dialer.go index eb6ffffa7..a696f26d3 100644 --- a/transport/internet/system_dialer.go +++ b/transport/internet/system_dialer.go @@ -63,10 +63,14 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne dest: destAddr, }, nil } - + goStdKeepAlive := time.Duration(0) + if sockopt.TcpKeepAliveIdle != 0 { + goStdKeepAlive = time.Duration(-1) + } dialer := &net.Dialer{ Timeout: time.Second * 16, LocalAddr: resolveSrcAddr(dest.Network, src), + KeepAlive: goStdKeepAlive, } if sockopt != nil || len(d.controllers) > 0 { diff --git a/transport/internet/system_listener.go b/transport/internet/system_listener.go index d59aa623d..73e19c118 100644 --- a/transport/internet/system_listener.go +++ b/transport/internet/system_listener.go @@ -4,6 +4,7 @@ import ( "context" "runtime" "syscall" + "time" "github.com/pires/go-proxyproto" @@ -49,6 +50,9 @@ func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *S network = addr.Network() address = addr.String() lc.Control = getControlFunc(ctx, sockopt, dl.controllers) + if sockopt.TcpKeepAliveIdle != 0 { + lc.KeepAlive = time.Duration(-1) + } case *net.UnixAddr: lc.Control = nil network = addr.Network()