1
1
package internet
2
2
3
3
import (
4
- "syscall "
4
+ "golang.org/x/sys/unix "
5
5
)
6
6
7
7
const (
8
- // TCP_FASTOPEN is the socket option on darwin for TCP fast open.
9
- TCP_FASTOPEN = 0x105
10
8
// TCP_FASTOPEN_SERVER is the value to enable TCP fast open on darwin for server connections.
11
9
TCP_FASTOPEN_SERVER = 0x01
12
10
// TCP_FASTOPEN_CLIENT is the value to enable TCP fast open on darwin for client connections.
13
- TCP_FASTOPEN_CLIENT = 0x02
11
+ TCP_FASTOPEN_CLIENT = 0x02 // nolint: revive,stylecheck
12
+ // syscall.TCP_KEEPINTVL is missing on some darwin architectures.
13
+ sysTCP_KEEPINTVL = 0x101 // nolint: revive,stylecheck
14
14
)
15
15
16
16
func applyOutboundSocketOptions (network string , address string , fd uintptr , config * SocketConfig ) error {
@@ -20,10 +20,30 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf
20
20
tfo = TCP_FASTOPEN_CLIENT
21
21
}
22
22
if tfo >= 0 {
23
- if err := syscall .SetsockoptInt (int (fd ), syscall .IPPROTO_TCP , TCP_FASTOPEN , tfo ); err != nil {
23
+ if err := unix .SetsockoptInt (int (fd ), unix .IPPROTO_TCP , unix . TCP_FASTOPEN , tfo ); err != nil {
24
24
return err
25
25
}
26
26
}
27
+
28
+ if config .TcpKeepAliveIdle > 0 || config .TcpKeepAliveInterval > 0 {
29
+ if config .TcpKeepAliveIdle > 0 {
30
+ if err := unix .SetsockoptInt (int (fd ), unix .IPPROTO_TCP , unix .TCP_KEEPALIVE , int (config .TcpKeepAliveInterval )); err != nil {
31
+ return newError ("failed to set TCP_KEEPINTVL" , err )
32
+ }
33
+ }
34
+ if config .TcpKeepAliveInterval > 0 {
35
+ if err := unix .SetsockoptInt (int (fd ), unix .IPPROTO_TCP , sysTCP_KEEPINTVL , int (config .TcpKeepAliveIdle )); err != nil {
36
+ return newError ("failed to set TCP_KEEPIDLE" , err )
37
+ }
38
+ }
39
+ if err := unix .SetsockoptInt (int (fd ), unix .SOL_SOCKET , unix .SO_KEEPALIVE , 1 ); err != nil {
40
+ return newError ("failed to set SO_KEEPALIVE" , err )
41
+ }
42
+ } else if config .TcpKeepAliveInterval < 0 || config .TcpKeepAliveIdle < 0 {
43
+ if err := unix .SetsockoptInt (int (fd ), unix .SOL_SOCKET , unix .SO_KEEPALIVE , 0 ); err != nil {
44
+ return newError ("failed to unset SO_KEEPALIVE" , err )
45
+ }
46
+ }
27
47
}
28
48
29
49
return nil
@@ -36,10 +56,29 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
36
56
tfo = TCP_FASTOPEN_SERVER
37
57
}
38
58
if tfo >= 0 {
39
- if err := syscall .SetsockoptInt (int (fd ), syscall .IPPROTO_TCP , TCP_FASTOPEN , tfo ); err != nil {
59
+ if err := unix .SetsockoptInt (int (fd ), unix .IPPROTO_TCP , unix . TCP_FASTOPEN , tfo ); err != nil {
40
60
return err
41
61
}
42
62
}
63
+ if config .TcpKeepAliveIdle > 0 || config .TcpKeepAliveInterval > 0 {
64
+ if config .TcpKeepAliveIdle > 0 {
65
+ if err := unix .SetsockoptInt (int (fd ), unix .IPPROTO_TCP , unix .TCP_KEEPALIVE , int (config .TcpKeepAliveInterval )); err != nil {
66
+ return newError ("failed to set TCP_KEEPINTVL" , err )
67
+ }
68
+ }
69
+ if config .TcpKeepAliveInterval > 0 {
70
+ if err := unix .SetsockoptInt (int (fd ), unix .IPPROTO_TCP , sysTCP_KEEPINTVL , int (config .TcpKeepAliveIdle )); err != nil {
71
+ return newError ("failed to set TCP_KEEPIDLE" , err )
72
+ }
73
+ }
74
+ if err := unix .SetsockoptInt (int (fd ), unix .SOL_SOCKET , unix .SO_KEEPALIVE , 1 ); err != nil {
75
+ return newError ("failed to set SO_KEEPALIVE" , err )
76
+ }
77
+ } else if config .TcpKeepAliveInterval < 0 || config .TcpKeepAliveIdle < 0 {
78
+ if err := unix .SetsockoptInt (int (fd ), unix .SOL_SOCKET , unix .SO_KEEPALIVE , 0 ); err != nil {
79
+ return newError ("failed to unset SO_KEEPALIVE" , err )
80
+ }
81
+ }
43
82
}
44
83
45
84
return nil
0 commit comments