diff --git a/network/v2/sockopts_congestion.go b/network/v2/sockopts_congestion.go new file mode 100644 index 0000000..30c7e12 --- /dev/null +++ b/network/v2/sockopts_congestion.go @@ -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 + }) +} diff --git a/network/v2/sockopts_congestion_stub.go b/network/v2/sockopts_congestion_stub.go new file mode 100644 index 0000000..bb7540c --- /dev/null +++ b/network/v2/sockopts_congestion_stub.go @@ -0,0 +1,7 @@ +//go:build !linux + +package network + +import "syscall" + +func setCongestionControl(conn syscall.RawConn) {} diff --git a/network/v2/sockopts_unix.go b/network/v2/sockopts_reuseaddr.go similarity index 96% rename from network/v2/sockopts_unix.go rename to network/v2/sockopts_reuseaddr.go index 75df3af..29bfcb4 100644 --- a/network/v2/sockopts_unix.go +++ b/network/v2/sockopts_reuseaddr.go @@ -1,5 +1,4 @@ //go:build !windows -// +build !windows package network diff --git a/network/v2/sockopts_windows.go b/network/v2/sockopts_reuseaddr_stub.go similarity index 87% rename from network/v2/sockopts_windows.go rename to network/v2/sockopts_reuseaddr_stub.go index 32a702a..b8cd910 100644 --- a/network/v2/sockopts_windows.go +++ b/network/v2/sockopts_reuseaddr_stub.go @@ -1,5 +1,4 @@ //go:build windows -// +build windows package network diff --git a/network/v2/sockopts_linux.go b/network/v2/sockopts_usertimeout.go similarity index 72% rename from network/v2/sockopts_linux.go rename to network/v2/sockopts_usertimeout.go index 60224e6..0a1b04d 100644 --- a/network/v2/sockopts_linux.go +++ b/network/v2/sockopts_usertimeout.go @@ -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 }) } - -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 - }) -} diff --git a/network/v2/sockopts_nolinux.go b/network/v2/sockopts_usertimeout_stub.go similarity index 65% rename from network/v2/sockopts_nolinux.go rename to network/v2/sockopts_usertimeout_stub.go index f2d5941..2d026c0 100644 --- a/network/v2/sockopts_nolinux.go +++ b/network/v2/sockopts_usertimeout_stub.go @@ -7,5 +7,4 @@ import ( "syscall" ) -func setCongestionControl(conn syscall.RawConn) {} func setTCPUserTimeout(conn syscall.RawConn, cfg net.KeepAliveConfig) {}