listen_reuseport.go 603 B

123456789101112131415161718192021222324252627282930313233
  1. // +build linux darwin
  2. // +build go1.11
  3. package main
  4. import (
  5. "context"
  6. "net"
  7. "syscall"
  8. "golang.org/x/sys/unix"
  9. )
  10. func listenReuseport(network, address string) (net.Listener, error) {
  11. if !conf.SoReuseport {
  12. return net.Listen(network, address)
  13. }
  14. lc := net.ListenConfig{
  15. Control: func(_, _ string, c syscall.RawConn) error {
  16. var cerr error
  17. err := c.Control(func(fd uintptr) {
  18. cerr = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
  19. })
  20. if err != nil {
  21. return err
  22. }
  23. return cerr
  24. },
  25. }
  26. return lc.Listen(context.Background(), network, address)
  27. }