Merge pull request #213 from 9seconds/fix-windows-build

Fix windows build
This commit is contained in:
Sergey Arkhipov
2021-10-04 10:20:13 +03:00
committed by GitHub
3 changed files with 45 additions and 16 deletions
+4 -16
View File
@@ -3,8 +3,6 @@ package network
import (
"fmt"
"net"
"golang.org/x/sys/unix"
)
// SetClientSocketOptions tunes a TCP socket that represents a connection to
@@ -50,22 +48,12 @@ func setCommonSocketOptions(conn *net.TCPConn, bufferSize int) error {
rawConn, err := conn.SyscallConn()
if err != nil {
return fmt.Errorf("cannot get underlying raw connection")
return fmt.Errorf("cannot get underlying raw connection: %w", err)
}
rawConn.Control(func(fd uintptr) { // nolint: errcheck
err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
if err != nil {
err = fmt.Errorf("cannot set SO_REUSEADDR: %w", err)
return
}
err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
if err != nil {
err = fmt.Errorf("cannot set SO_REUSEPORT: %w", err)
}
})
if err := setSocketReuseAddrPort(rawConn, bufferSize); err != nil {
return fmt.Errorf("cannot setup SO_REUSEADDR/PORT: %w", err)
}
return nil
}
+31
View File
@@ -0,0 +1,31 @@
//go:build !windows
// +build !windows
package network
import (
"fmt"
"syscall"
"golang.org/x/sys/unix"
)
func setSocketReuseAddrPort(conn syscall.RawConn, bufferSize int) error {
var err error
conn.Control(func(fd uintptr) { // nolint: errcheck
err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
if err != nil {
err = fmt.Errorf("cannot set SO_REUSEADDR: %w", err)
return
}
err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
if err != nil {
err = fmt.Errorf("cannot set SO_REUSEPORT: %w", err)
}
})
return err
}
+10
View File
@@ -0,0 +1,10 @@
//go:build windows
// +build windows
package network
import "syscall"
func setSocketReuseAddrPort(conn syscall.RawConn, bufferSize int) error {
return nil
}