瀏覽代碼

Use TCP BBR in a best-effort mode

This small commits conditionally sets TCP BBR as a preferrable algorithm
for TCP congestion control
pull/455/head
9seconds 4 週之前
父節點
當前提交
a7edff4207
共有 3 個檔案被更改,包括 28 行新增0 行删除
  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 查看文件

@@ -23,5 +23,7 @@ func setCommonSocketOptions(conn *net.TCPConn, keepAliveConfig net.KeepAliveConf
23 23
 		return fmt.Errorf("cannot setup SO_REUSEADDR/PORT: %w", err)
24 24
 	}
25 25
 
26
+	setCongestionControl(rawConn)
27
+
26 28
 	return nil
27 29
 }

+ 19
- 0
network/v2/sockopts_linux.go 查看文件

@@ -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 查看文件

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

Loading…
取消
儲存