Browse Source

Use TCP BBR in a best-effort mode

This small commits conditionally sets TCP BBR as a preferrable algorithm
for TCP congestion control
tags/v2.2.8^2^2
9seconds 4 weeks ago
parent
commit
88f33debab
3 changed files with 28 additions and 0 deletions
  1. 2
    0
      network/v2/sockopts.go
  2. 19
    0
      network/v2/sockopts_linux.go
  3. 7
    0
      network/v2/sockopts_nolinux.go

+ 2
- 0
network/v2/sockopts.go View File

@@ -28,5 +28,7 @@ func setCommonSocketOptions(conn *net.TCPConn) error {
28 28
 		return fmt.Errorf("cannot setup SO_REUSEADDR/PORT: %w", err)
29 29
 	}
30 30
 
31
+	setCongestionControl(rawConn)
32
+
31 33
 	return nil
32 34
 }

+ 19
- 0
network/v2/sockopts_linux.go View File

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

+ 7
- 0
network/v2/sockopts_nolinux.go View File

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

Loading…
Cancel
Save