Przeglądaj źródła

Merge pull request #459 from dolonet/fix/openbsd-keepalive

Fix TCP keepalive setup on OpenBSD
pull/467/head
Sergei Arkhipov 3 tygodni temu
rodzic
commit
d7249756e2
No account linked to committer's email address

+ 1
- 1
network/sockopts.go Wyświetl plik

20
 }
20
 }
21
 
21
 
22
 func setCommonSocketOptions(conn *net.TCPConn) error {
22
 func setCommonSocketOptions(conn *net.TCPConn) error {
23
-	if err := conn.SetKeepAliveConfig(net.KeepAliveConfig{
23
+	if err := applyKeepAlive(conn, net.KeepAliveConfig{
24
 		Enable:   true,
24
 		Enable:   true,
25
 		Idle:     DefaultKeepAliveIdle,
25
 		Idle:     DefaultKeepAliveIdle,
26
 		Interval: DefaultKeepAliveInterval,
26
 		Interval: DefaultKeepAliveInterval,

+ 11
- 0
network/sockopts_keepalive.go Wyświetl plik

1
+//go:build !openbsd
2
+
3
+package network
4
+
5
+import "net"
6
+
7
+// applyKeepAlive enables TCP keepalive on conn and applies the per-socket
8
+// idle/interval/count tuning from cfg.
9
+func applyKeepAlive(conn *net.TCPConn, cfg net.KeepAliveConfig) error {
10
+	return conn.SetKeepAliveConfig(cfg) //nolint: wrapcheck
11
+}

+ 20
- 0
network/sockopts_keepalive_openbsd.go Wyświetl plik

1
+package network
2
+
3
+import "net"
4
+
5
+// applyKeepAlive enables (or disables) TCP keepalive on conn.
6
+//
7
+// OpenBSD has no user-settable per-socket TCP keepalive options: TCP_KEEPIDLE,
8
+// TCP_KEEPINTVL and TCP_KEEPCNT do not exist on OpenBSD, and Go's
9
+// (*TCPConn).SetKeepAliveConfig therefore returns ENOPROTOOPT ("protocol not
10
+// available") for any non-negative Idle/Interval/Count value (see
11
+// src/net/tcpsockopt_openbsd.go in the Go source tree). Calling
12
+// SetKeepAliveConfig with mtg's defaults (zero values) breaks every accepted
13
+// listener connection and every outbound dial on OpenBSD.
14
+//
15
+// On OpenBSD we only flip SO_KEEPALIVE on or off; the keepalive timing is
16
+// controlled system-wide via the sysctl knobs net.inet.tcp.keepidle and
17
+// net.inet.tcp.keepintvl.
18
+func applyKeepAlive(conn *net.TCPConn, cfg net.KeepAliveConfig) error {
19
+	return conn.SetKeepAlive(cfg.Enable) //nolint: wrapcheck
20
+}

+ 1
- 1
network/v2/sockopts.go Wyświetl plik

6
 )
6
 )
7
 
7
 
8
 func setCommonSocketOptions(conn *net.TCPConn, keepAliveConfig net.KeepAliveConfig) error {
8
 func setCommonSocketOptions(conn *net.TCPConn, keepAliveConfig net.KeepAliveConfig) error {
9
-	if err := conn.SetKeepAliveConfig(keepAliveConfig); err != nil {
9
+	if err := applyKeepAlive(conn, keepAliveConfig); err != nil {
10
 		return fmt.Errorf("cannot configure TCP keepalive: %w", err)
10
 		return fmt.Errorf("cannot configure TCP keepalive: %w", err)
11
 	}
11
 	}
12
 
12
 

+ 11
- 0
network/v2/sockopts_keepalive.go Wyświetl plik

1
+//go:build !openbsd
2
+
3
+package network
4
+
5
+import "net"
6
+
7
+// applyKeepAlive enables TCP keepalive on conn and applies the per-socket
8
+// idle/interval/count tuning from cfg.
9
+func applyKeepAlive(conn *net.TCPConn, cfg net.KeepAliveConfig) error {
10
+	return conn.SetKeepAliveConfig(cfg) //nolint: wrapcheck
11
+}

+ 20
- 0
network/v2/sockopts_keepalive_openbsd.go Wyświetl plik

1
+package network
2
+
3
+import "net"
4
+
5
+// applyKeepAlive enables (or disables) TCP keepalive on conn.
6
+//
7
+// OpenBSD has no user-settable per-socket TCP keepalive options: TCP_KEEPIDLE,
8
+// TCP_KEEPINTVL and TCP_KEEPCNT do not exist on OpenBSD, and Go's
9
+// (*TCPConn).SetKeepAliveConfig therefore returns ENOPROTOOPT ("protocol not
10
+// available") for any non-negative Idle/Interval/Count value (see
11
+// src/net/tcpsockopt_openbsd.go in the Go source tree). Calling
12
+// SetKeepAliveConfig with mtg's defaults (zero values) breaks every accepted
13
+// listener connection and every outbound dial on OpenBSD.
14
+//
15
+// On OpenBSD we only flip SO_KEEPALIVE on or off; the keepalive timing is
16
+// controlled system-wide via the sysctl knobs net.inet.tcp.keepidle and
17
+// net.inet.tcp.keepintvl.
18
+func applyKeepAlive(conn *net.TCPConn, cfg net.KeepAliveConfig) error {
19
+	return conn.SetKeepAlive(cfg.Enable) //nolint: wrapcheck
20
+}

Ładowanie…
Anuluj
Zapisz