github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/net/internal/netpoll/netpoll_unix.go (about) 1 // +build linux netbsd freebsd dragonfly 2 3 package netpoll 4 5 import "golang.org/x/sys/unix" 6 7 // SetKeepAlive sets the keepalive for the connection. 8 func SetKeepAlive(fd, secs int) error { 9 if err := unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_KEEPALIVE, 1); err != nil { 10 return err 11 } 12 if err := unix.SetsockoptInt(fd, unix.IPPROTO_TCP, unix.TCP_KEEPINTVL, secs); err != nil { 13 return err 14 } 15 return unix.SetsockoptInt(fd, unix.IPPROTO_TCP, unix.TCP_KEEPIDLE, secs) 16 }