trpc.group/trpc-go/trpc-go@v1.0.3/internal/reuseport/reuseport_bsd.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  //go:build darwin || dragonfly || freebsd || netbsd || openbsd
    15  // +build darwin dragonfly freebsd netbsd openbsd
    16  
    17  package reuseport
    18  
    19  import (
    20  	"runtime"
    21  	"syscall"
    22  )
    23  
    24  var reusePort = syscall.SO_REUSEPORT
    25  
    26  func maxListenerBacklog() int {
    27  	var (
    28  		n   uint32
    29  		err error
    30  	)
    31  
    32  	switch runtime.GOOS {
    33  	case "darwin", "freebsd":
    34  		n, err = syscall.SysctlUint32("kern.ipc.somaxconn")
    35  	case "netbsd":
    36  		// NOTE: NetBSD has no somaxconn-like kernel state so far
    37  	case "openbsd":
    38  		n, err = syscall.SysctlUint32("kern.somaxconn")
    39  	default:
    40  	}
    41  
    42  	return defaultBacklog(n, err)
    43  }
    44  
    45  func defaultBacklog(n uint32, err error) int {
    46  	if n == 0 || err != nil {
    47  		return syscall.SOMAXCONN
    48  	}
    49  
    50  	// FreeBSD stores the backlog in a uint16, as does Linux.
    51  	// Assume the other BSDs do too. Truncate number to avoid wrapping.
    52  	// See issue 5030.
    53  	if n > 1<<16-1 {
    54  		n = 1<<16 - 1
    55  	}
    56  	return int(n)
    57  }