github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/conn/conn_reuseport.go (about)

     1  //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || zos
     2  
     3  package conn
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"golang.org/x/sys/unix"
     9  )
    10  
    11  func setReusePort(fd int) error {
    12  	if err := unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
    13  		return fmt.Errorf("failed to set socket option SO_REUSEPORT: %w", err)
    14  	}
    15  	return nil
    16  }
    17  
    18  func (fns setFuncSlice) appendSetReusePortFunc(reusePort bool) setFuncSlice {
    19  	if reusePort {
    20  		return append(fns, func(fd int, network string) error {
    21  			return setReusePort(fd)
    22  		})
    23  	}
    24  	return fns
    25  }