diff --git a/network/v2/sockopts.go b/network/v2/sockopts.go index e89b1ea..84563d7 100644 --- a/network/v2/sockopts.go +++ b/network/v2/sockopts.go @@ -28,5 +28,7 @@ func setCommonSocketOptions(conn *net.TCPConn) error { return fmt.Errorf("cannot setup SO_REUSEADDR/PORT: %w", err) } + setCongestionControl(rawConn) + return nil } diff --git a/network/v2/sockopts_linux.go b/network/v2/sockopts_linux.go new file mode 100644 index 0000000..802862e --- /dev/null +++ b/network/v2/sockopts_linux.go @@ -0,0 +1,19 @@ +//go:build linux + +package network + +import ( + "syscall" + + "golang.org/x/sys/unix" +) + +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_nolinux.go new file mode 100644 index 0000000..bb7540c --- /dev/null +++ b/network/v2/sockopts_nolinux.go @@ -0,0 +1,7 @@ +//go:build !linux + +package network + +import "syscall" + +func setCongestionControl(conn syscall.RawConn) {}