Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport TCP backlog size update of uint16->uint32 with Linux #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 60 additions & 4 deletions tcplisten_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,69 @@ func soMaxConn() (int, error) {
return -1, fmt.Errorf("cannot parse somaxconn %q read from %s: %s", s, soMaxConnFilePath, err)
}

// Linux stores the backlog in a uint16.
// Truncate number to avoid wrapping.
// See https://github.com/golang/go/issues/5030 .
if n > 1<<16-1 {
n = 1<<16 - 1
n = maxAckBacklog(n)
}
return n, nil
}

func kernelVersion() (major int, minor int) {
var uname syscall.Utsname
if err := syscall.Uname(&uname); err != nil {
return
}

rl := uname.Release
var values [2]int
vi := 0
value := 0
for _, c := range rl {
if c >= '0' && c <= '9' {
value = (value * 10) + int(c-'0')
} else {
// Note that we're assuming N.N.N here. If we see anything else we are likely to
// mis-parse it.
values[vi] = value
vi++
if vi >= len(values) {
break
}
}
}
switch vi {
case 0:
return 0, 0
case 1:
return values[0], 0
case 2:
return values[0], values[1]
}
return
}

// Linux stores the backlog as:
//
// - uint16 in kernel version < 4.1,
// - uint32 in kernel version >= 4.1
//
// Truncate number to avoid wrapping.
//
// See issue 5 or
// https://github.com/golang/go/issues/5030.
// https://github.com/golang/go/issues/41470.
func maxAckBacklog(n int) int {
major, minor := kernelVersion()
size := 16
if major > 4 || (major == 4 && minor >= 1) {
size = 32
}

var max uint = 1<<size - 1
if uint(n) > max {
n = int(max)
}
return n
}


const soMaxConnFilePath = "/proc/sys/net/core/somaxconn"