Source file src/net/tcpsockopt_unix.go

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build aix || dragonfly || freebsd || illumos || linux || netbsd
     6  
     7  package net
     8  
     9  import (
    10  	"runtime"
    11  	"syscall"
    12  	"time"
    13  )
    14  
    15  func setKeepAliveIdle(fd *netFD, d time.Duration) error {
    16  	if d == 0 {
    17  		d = defaultTCPKeepAliveIdle
    18  	} else if d < 0 {
    19  		return nil
    20  	}
    21  
    22  	// The kernel expects seconds so round to next highest second.
    23  	secs := int(roundDurationUp(d, time.Second))
    24  	err := fd.pfd.SetsockoptInt(syscall.IPPROTO_TCP, syscall.TCP_KEEPIDLE, secs)
    25  	runtime.KeepAlive(fd)
    26  	return wrapSyscallError("setsockopt", err)
    27  }
    28  
    29  func setKeepAliveInterval(fd *netFD, d time.Duration) error {
    30  	if d == 0 {
    31  		d = defaultTCPKeepAliveInterval
    32  	} else if d < 0 {
    33  		return nil
    34  	}
    35  
    36  	// The kernel expects seconds so round to next highest second.
    37  	secs := int(roundDurationUp(d, time.Second))
    38  	err := fd.pfd.SetsockoptInt(syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, secs)
    39  	runtime.KeepAlive(fd)
    40  	return wrapSyscallError("setsockopt", err)
    41  }
    42  
    43  func setKeepAliveCount(fd *netFD, n int) error {
    44  	if n == 0 {
    45  		n = defaultTCPKeepAliveCount
    46  	} else if n < 0 {
    47  		return nil
    48  	}
    49  
    50  	err := fd.pfd.SetsockoptInt(syscall.IPPROTO_TCP, syscall.TCP_KEEPCNT, n)
    51  	runtime.KeepAlive(fd)
    52  	return wrapSyscallError("setsockopt", err)
    53  }
    54  

View as plain text