diff --git a/sockopt.go b/sockopt.go index 3ad4bdc..5514829 100644 --- a/sockopt.go +++ b/sockopt.go @@ -8,7 +8,8 @@ func SetTFOListener(fd uintptr) error { } // SetTFOListenerWithBacklog enables TCP Fast Open on the listener with the given backlog. -// If the platform does not support custom backlog, the specified backlog is ignored. +// If the backlog is 0, Go std's listen(2) backlog is used. +// If the platform does not support custom backlog values, the given backlog is ignored. func SetTFOListenerWithBacklog(fd uintptr, backlog int) error { return setTFOListenerWithBacklog(fd, backlog) // sockopt_linux.go, sockopt_listen_generic.go, sockopt_stub.go } diff --git a/sockopt_linux.go b/sockopt_linux.go index b97effd..ad726fd 100644 --- a/sockopt_linux.go +++ b/sockopt_linux.go @@ -1,22 +1,34 @@ package tfo -import "golang.org/x/sys/unix" +import ( + _ "unsafe" + + "golang.org/x/sys/unix" +) // TCPFastopenQueueLength is the maximum number of total pending TFO connection requests, // see https://datatracker.ietf.org/doc/html/rfc7413#section-5.1 for why this limit exists. -// The current value aligns with Go std's listen(2) backlog (4096, as of the current version). +// The current value is the default net.core.somaxconn on Linux. // // Deprecated: This constant is no longer used in this module and will be removed in v3. const TCPFastopenQueueLength = 4096 func setTFOListener(fd uintptr) error { - return setTFOListenerWithBacklog(fd, defaultBacklog) + return setTFOListenerWithBacklog(fd, 0) } func setTFOListenerWithBacklog(fd uintptr, backlog int) error { + if backlog == 0 { + backlog = listenerBacklog() + } return setTFO(int(fd), backlog) } +// listenerBacklog is linked from src/net/net.go +// +//go:linkname listenerBacklog net.listenerBacklog +func listenerBacklog() int + func setTFODialer(fd uintptr) error { return unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_FASTOPEN_CONNECT, 1) } diff --git a/tfo.go b/tfo.go index 42b8ed0..29790fb 100644 --- a/tfo.go +++ b/tfo.go @@ -42,7 +42,7 @@ type ListenConfig struct { net.ListenConfig // Backlog specifies the maximum number of pending TFO connections on supported platforms. - // If the value is 0, Go std's listen(2) backlog (4096, as of the current version) is used. + // If the value is 0, Go std's listen(2) backlog is used. // If the value is negative, TFO is disabled. Backlog int diff --git a/tfo_listen_generic.go b/tfo_listen_generic.go index 073765a..8277e5a 100644 --- a/tfo_listen_generic.go +++ b/tfo_listen_generic.go @@ -9,16 +9,9 @@ import ( "syscall" ) -// defaultBacklog is Go std's listen(2) backlog. -// We use this as the default TFO backlog. -const defaultBacklog = 4096 - func (lc *ListenConfig) listenTFO(ctx context.Context, network, address string) (net.Listener, error) { ctrlFn := lc.Control backlog := lc.Backlog - if backlog == 0 { - backlog = defaultBacklog - } llc := *lc llc.Control = func(network, address string, c syscall.RawConn) (err error) { if ctrlFn != nil {