Browse Source

Fix windows build

tags/v2.1.2^2
9seconds 4 years ago
parent
commit
d1e5f9d145
3 changed files with 44 additions and 15 deletions
  1. 3
    15
      network/sockopts.go
  2. 31
    0
      network/sockopts_unix.go
  3. 10
    0
      network/sockopts_windows.go

+ 3
- 15
network/sockopts.go View File

@@ -3,8 +3,6 @@ package network
3 3
 import (
4 4
 	"fmt"
5 5
 	"net"
6
-
7
-	"golang.org/x/sys/unix"
8 6
 )
9 7
 
10 8
 // SetClientSocketOptions tunes a TCP socket that represents a connection to
@@ -53,19 +51,9 @@ func setCommonSocketOptions(conn *net.TCPConn, bufferSize int) error {
53 51
 		return fmt.Errorf("cannot get underlying raw connection: %w", err)
54 52
 	}
55 53
 
56
-	rawConn.Control(func(fd uintptr) { // nolint: errcheck
57
-		err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
58
-		if err != nil {
59
-			err = fmt.Errorf("cannot set SO_REUSEADDR: %w", err)
60
-
61
-			return
62
-		}
63
-
64
-		err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
65
-		if err != nil {
66
-			err = fmt.Errorf("cannot set SO_REUSEPORT: %w", err)
67
-		}
68
-	})
54
+	if err := setSocketReuseAddrPort(rawConn, bufferSize); err != nil {
55
+		return fmt.Errorf("cannot setup SO_REUSEADDR/PORT: %w", err)
56
+	}
69 57
 
70 58
 	return nil
71 59
 }

+ 31
- 0
network/sockopts_unix.go View File

@@ -0,0 +1,31 @@
1
+//go:build !windows
2
+// +build !windows
3
+
4
+package network
5
+
6
+import (
7
+	"fmt"
8
+	"syscall"
9
+
10
+	"golang.org/x/sys/unix"
11
+)
12
+
13
+func setSocketReuseAddrPort(conn syscall.RawConn, bufferSize int) error {
14
+	var err error
15
+
16
+	conn.Control(func(fd uintptr) { // nolint: errcheck
17
+		err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
18
+		if err != nil {
19
+			err = fmt.Errorf("cannot set SO_REUSEADDR: %w", err)
20
+
21
+			return
22
+		}
23
+
24
+		err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
25
+		if err != nil {
26
+			err = fmt.Errorf("cannot set SO_REUSEPORT: %w", err)
27
+		}
28
+	})
29
+
30
+	return err
31
+}

+ 10
- 0
network/sockopts_windows.go View File

@@ -0,0 +1,10 @@
1
+//go:build windows
2
+// +build windows
3
+
4
+package network
5
+
6
+import "syscall"
7
+
8
+func setSocketReuseAddrPort(conn syscall.RawConn, bufferSize int) error {
9
+	return nil
10
+}

Loading…
Cancel
Save