github.com/Cloud-Foundations/Dominator@v0.3.4/lib/net/tcpsockopt_unix.go (about)

     1  // +build freebsd linux netbsd
     2  
     3  package net
     4  
     5  import (
     6  	"syscall"
     7  	"time"
     8  )
     9  
    10  func (conn *connection) SetKeepAlivePeriod(d time.Duration) error {
    11  	// The kernel expects seconds so round to next highest second.
    12  	d += (time.Second - time.Nanosecond)
    13  	secs := int(d.Seconds())
    14  	err := syscall.SetsockoptInt(conn.fd, syscall.IPPROTO_TCP,
    15  		syscall.TCP_KEEPINTVL, secs)
    16  	if err != nil {
    17  		return err
    18  	}
    19  	err = syscall.SetsockoptInt(conn.fd, syscall.IPPROTO_TCP,
    20  		syscall.TCP_KEEPIDLE, secs)
    21  	if err != nil {
    22  		return err
    23  	}
    24  	return syscall.SetsockoptInt(conn.fd, syscall.IPPROTO_TCP,
    25  		syscall.TCP_KEEPCNT, 2)
    26  }