tcplisten icon indicating copy to clipboard operation
tcplisten copied to clipboard

tcp backlog size truncated to max uint16 instead of uint32

Open lexand opened this issue 4 years ago • 4 comments

see related https://github.com/golang/go/issues/41470

$ sudo sysctl -w net.core.somaxconn=196602
net.core.somaxconn = 196602
$ uname -a
Linux alex-work 4.15.0-117-generic #118-Ubuntu SMP Fri Sep 4 20:02:41 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
package main

import (
	"net/http"

	"github.com/valyala/fasthttp"
	"github.com/valyala/tcplisten"
)

func main() {
	s := &fasthttp.Server{
		DisableKeepalive:             true,
		DisablePreParseMultipartForm: true,
		Handler: func(ctx *fasthttp.RequestCtx) {
			ctx.SetStatusCode(http.StatusNoContent)
		},
	}

	tcpCfg := &tcplisten.Config{
		ReusePort:   true,
		DeferAccept: false,
		FastOpen:    true,
	}
	l, _ := tcpCfg.NewListener("tcp4", "0.0.0.0:8888")
	_ = s.Serve(l)
}

Expected

$ ss -l | grep 8888
tcp               LISTEN              0                    196602                                                                                        0.0.0.0:8888                                                 0.0.0.0:*  

Actual

$ ss -l | grep 8888
tcp               LISTEN              0                    65535                                                                                        0.0.0.0:8888                                                 0.0.0.0:*  

But this code works well

```go
package main

import (
	"net/http"

	"github.com/valyala/fasthttp"
	"github.com/valyala/tcplisten"
)

func main() {
	s := &fasthttp.Server{
		DisableKeepalive:             true,
		DisablePreParseMultipartForm: true,
		Handler: func(ctx *fasthttp.RequestCtx) {
			ctx.SetStatusCode(http.StatusNoContent)
		},
	}

	tcpCfg := &tcplisten.Config{
		ReusePort:   true,
		DeferAccept: false,
		FastOpen:    true,
                Backlog:     196602,
	}
	l, _ := tcpCfg.NewListener("tcp4", "0.0.0.0:8888")
	_ = s.Serve(l)
}

$ ss -l | grep 8888
tcp               LISTEN              0                    196602                                                                                        0.0.0.0:8888                                                 0.0.0.0:*  

lexand avatar Sep 18 '20 09:09 lexand

Hi, @lexand Because valyala/tcplisten calls syscall.Listen(fd, backlog) directly now, It can be set backlog size under system requirements if we use tcplisten.Config.Backlog. Unless we use config, it adopt soMaxConn for backlog. Backlog size is truncate to max(uint16)-1 by soMaxConn which is same behavior to net.Listen as you mentioned the issue(https://github.com/golang/go/issues/41470)

u5surf avatar Dec 01 '20 12:12 u5surf

Hi, @u5surf

In issue(golang/go#41470) and in this issue(#5) I point only that code has not actual behaviour for new linux kernels. As new kernels store so_max_conn in uint32.

Default truncating to truncate to max(uint16)-1 is actual for very old kernels.

lexand avatar Dec 01 '20 13:12 lexand

I had backported https://go-review.googlesource.com/c/go/+/255898/ check it out #6

u5surf avatar Dec 04 '20 00:12 u5surf

thats works as expected thanks

lexand avatar Dec 04 '20 13:12 lexand