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  	size := 16
    22  	if unix.KernelVersionGE(4, 1) {
    23  		size = 32
    24  	}
    25  
    26  	var max uint = 1<<size - 1
    27  	if uint(n) > max {
    28  		n = int(max)
    29  	}
    30  	return n
    31  }
    32  
    33  func maxListenerBacklog() int {
    34  	fd, err := open("/proc/sys/net/core/somaxconn")
    35  	if err != nil {
    36  		return syscall.SOMAXCONN
    37  	}
    38  	defer fd.close()
    39  	l, ok := fd.readLine()
    40  	if !ok {
    41  		return syscall.SOMAXCONN
    42  	}
    43  	f := getFields(l)
    44  	n, _, ok := dtoi(f[0])
    45  	if n == 0 || !ok {
    46  		return syscall.SOMAXCONN
    47  	}
    48  
    49  	if n > 1<<16-1 {
    50  		return maxAckBacklog(n)
    51  	}
    52  	return n
    53  }
    54  

View as plain text