Source file src/net/sock_linux.go

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package net
     6  
     7  import (
     8  	"internal/syscall/unix"
     9  	"syscall"
    10  )
    11  
    12  // Linux stores the backlog as:
    13  //
    14  //   - uint16 in kernel version < 4.1,
    15  //   - uint32 in kernel version >= 4.1
    16  //
    17  // Truncate number to avoid wrapping.
    18  //
    19  // See issue 5030 and 41470.
    20  func maxAckBacklog(n int) int {
    21  	major, minor := unix.KernelVersion()
    22  	size := 16
    23  	if major > 4 || (major == 4 && minor >= 1) {
    24  		size = 32
    25  	}
    26  
    27  	var max uint = 1<<size - 1
    28  	if uint(n) > max {
    29  		n = int(max)
    30  	}
    31  	return n
    32  }
    33  
    34  func maxListenerBacklog() int {
    35  	fd, err := open("/proc/sys/net/core/somaxconn")
    36  	if err != nil {
    37  		return syscall.SOMAXCONN
    38  	}
    39  	defer fd.close()
    40  	l, ok := fd.readLine()
    41  	if !ok {
    42  		return syscall.SOMAXCONN
    43  	}
    44  	f := getFields(l)
    45  	n, _, ok := dtoi(f[0])
    46  	if n == 0 || !ok {
    47  		return syscall.SOMAXCONN
    48  	}
    49  
    50  	if n > 1<<16-1 {
    51  		return maxAckBacklog(n)
    52  	}
    53  	return n
    54  }
    55  

View as plain text