Sfoglia il codice sorgente

Merge pull request #454 from 9seconds/tcp-notsent-lowat

Add TCP_NOTSENT_LOWAT setting
tags/v2.2.8^2^2
Sergei Arkhipov 4 settimane fa
parent
commit
b5799500e4
Nessun account collegato all'indirizzo email del committer

+ 8
- 0
network/v2/init.go Vedi File

@@ -55,6 +55,14 @@ const (
55 55
 	// tcpLingerTimeout defines a number of seconds to wait for sending
56 56
 	// unacknowledged data.
57 57
 	tcpLingerTimeout = 1
58
+
59
+	// tcpNotSentLowat limits the amount of unsent data queued in the
60
+	// kernel write buffer per socket. When the unsent data drops below
61
+	// this threshold, the socket becomes writable again. This reduces
62
+	// per-connection memory usage and bufferbloat by applying
63
+	// back-pressure to the relay loop instead of piling up data in
64
+	// kernel buffers.
65
+	tcpNotSentLowat = 128 * 1024
58 66
 )
59 67
 
60 68
 var (

+ 1
- 0
network/v2/sockopts.go Vedi File

@@ -25,6 +25,7 @@ func setCommonSocketOptions(conn *net.TCPConn, keepAliveConfig net.KeepAliveConf
25 25
 
26 26
 	setCongestionControl(rawConn)
27 27
 	setTCPUserTimeout(rawConn, keepAliveConfig)
28
+	setNotSentLowat(rawConn)
28 29
 
29 30
 	return nil
30 31
 }

+ 20
- 0
network/v2/sockopts_congestion.go Vedi File

@@ -0,0 +1,20 @@
1
+//go:build linux
2
+
3
+package network
4
+
5
+import (
6
+	"syscall"
7
+
8
+	"golang.org/x/sys/unix"
9
+)
10
+
11
+// setCongestionControl sets BBR as the TCP congestion control algorithm.
12
+// BBR provides better throughput over lossy and high-latency links compared
13
+// to the default cubic, which is especially beneficial for mobile and
14
+// home internet clients. This is best-effort: silently ignored if the
15
+// kernel does not have tcp_bbr available.
16
+func setCongestionControl(conn syscall.RawConn) {
17
+	conn.Control(func(fd uintptr) { //nolint: errcheck
18
+		unix.SetsockoptString(int(fd), unix.IPPROTO_TCP, unix.TCP_CONGESTION, "bbr") //nolint: errcheck
19
+	})
20
+}

+ 7
- 0
network/v2/sockopts_congestion_stub.go Vedi File

@@ -0,0 +1,7 @@
1
+//go:build !linux
2
+
3
+package network
4
+
5
+import "syscall"
6
+
7
+func setCongestionControl(conn syscall.RawConn) {}

+ 20
- 0
network/v2/sockopts_lowat.go Vedi File

@@ -0,0 +1,20 @@
1
+//go:build linux || darwin
2
+
3
+package network
4
+
5
+import (
6
+	"syscall"
7
+
8
+	"golang.org/x/sys/unix"
9
+)
10
+
11
+// setNotSentLowat sets TCP_NOTSENT_LOWAT which limits the amount of
12
+// unsent data queued in the kernel write buffer. Once unsent data drops
13
+// below this threshold the socket becomes writable again, applying
14
+// back-pressure to the relay loop instead of piling up data in kernel
15
+// buffers. This reduces per-connection memory and bufferbloat.
16
+func setNotSentLowat(conn syscall.RawConn) {
17
+	conn.Control(func(fd uintptr) { //nolint: errcheck
18
+		unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_NOTSENT_LOWAT, tcpNotSentLowat) //nolint: errcheck
19
+	})
20
+}

+ 7
- 0
network/v2/sockopts_lowat_stub.go Vedi File

@@ -0,0 +1,7 @@
1
+//go:build !linux && !darwin
2
+
3
+package network
4
+
5
+import "syscall"
6
+
7
+func setNotSentLowat(conn syscall.RawConn) {}

network/v2/sockopts_unix.go → network/v2/sockopts_reuseaddr.go Vedi File

@@ -1,5 +1,4 @@
1 1
 //go:build !windows
2
-// +build !windows
3 2
 
4 3
 package network
5 4
 

network/v2/sockopts_windows.go → network/v2/sockopts_reuseaddr_stub.go Vedi File

@@ -1,5 +1,4 @@
1 1
 //go:build windows
2
-// +build windows
3 2
 
4 3
 package network
5 4
 

network/v2/sockopts_linux.go → network/v2/sockopts_usertimeout.go Vedi File

@@ -46,13 +46,3 @@ func setTCPUserTimeout(conn syscall.RawConn, cfg net.KeepAliveConfig) {
46 46
 		unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(timeout.Milliseconds())) //nolint: errcheck
47 47
 	})
48 48
 }
49
-
50
-func setCongestionControl(conn syscall.RawConn) {
51
-	conn.Control(func(fd uintptr) { //nolint: errcheck
52
-		// BBR provides better throughput over lossy and high-latency links compared
53
-		// to the default cubic, which is especially beneficial for mobile and
54
-		// home internet clients. This is best-effort: silently ignored if the
55
-		// kernel does not have tcp_bbr available.
56
-		unix.SetsockoptString(int(fd), unix.IPPROTO_TCP, unix.TCP_CONGESTION, "bbr") //nolint: errcheck
57
-	})
58
-}

network/v2/sockopts_nolinux.go → network/v2/sockopts_usertimeout_stub.go Vedi File

@@ -7,5 +7,4 @@ import (
7 7
 	"syscall"
8 8
 )
9 9
 
10
-func setCongestionControl(conn syscall.RawConn)                       {}
11 10
 func setTCPUserTimeout(conn syscall.RawConn, cfg net.KeepAliveConfig) {}

Loading…
Annulla
Salva