Refactor socksopts per functionality, not per build flag

This commit is contained in:
9seconds
2026-04-07 15:42:22 +02:00
parent 0de8b28de8
commit 437dacfaab
6 changed files with 27 additions and 13 deletions
+20
View File
@@ -0,0 +1,20 @@
//go:build linux
package network
import (
"syscall"
"golang.org/x/sys/unix"
)
// setCongestionControl sets BBR as the TCP congestion control algorithm.
// BBR provides better throughput over lossy and high-latency links compared
// to the default cubic, which is especially beneficial for mobile and
// home internet clients. This is best-effort: silently ignored if the
// kernel does not have tcp_bbr available.
func setCongestionControl(conn syscall.RawConn) {
conn.Control(func(fd uintptr) { //nolint: errcheck
unix.SetsockoptString(int(fd), unix.IPPROTO_TCP, unix.TCP_CONGESTION, "bbr") //nolint: errcheck
})
}
+7
View File
@@ -0,0 +1,7 @@
//go:build !linux
package network
import "syscall"
func setCongestionControl(conn syscall.RawConn) {}
@@ -1,5 +1,4 @@
//go:build !windows //go:build !windows
// +build !windows
package network package network
@@ -1,5 +1,4 @@
//go:build windows //go:build windows
// +build windows
package network package network
@@ -46,13 +46,3 @@ func setTCPUserTimeout(conn syscall.RawConn, cfg net.KeepAliveConfig) {
unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(timeout.Milliseconds())) //nolint: errcheck unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(timeout.Milliseconds())) //nolint: errcheck
}) })
} }
func setCongestionControl(conn syscall.RawConn) {
conn.Control(func(fd uintptr) { //nolint: errcheck
// BBR provides better throughput over lossy and high-latency links compared
// to the default cubic, which is especially beneficial for mobile and
// home internet clients. This is best-effort: silently ignored if the
// kernel does not have tcp_bbr available.
unix.SetsockoptString(int(fd), unix.IPPROTO_TCP, unix.TCP_CONGESTION, "bbr") //nolint: errcheck
})
}
@@ -7,5 +7,4 @@ import (
"syscall" "syscall"
) )
func setCongestionControl(conn syscall.RawConn) {}
func setTCPUserTimeout(conn syscall.RawConn, cfg net.KeepAliveConfig) {} func setTCPUserTimeout(conn syscall.RawConn, cfg net.KeepAliveConfig) {}