sock_bsd.go 835 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. // +build darwin dragonfly freebsd netbsd openbsd
  5. package net
  6. import (
  7. "runtime"
  8. "syscall"
  9. )
  10. func maxListenerBacklog() int {
  11. var (
  12. n uint32
  13. err error
  14. )
  15. switch runtime.GOOS {
  16. case "darwin", "freebsd":
  17. n, err = syscall.SysctlUint32("kern.ipc.somaxconn")
  18. case "netbsd":
  19. // NOTE: NetBSD has no somaxconn-like kernel state so far
  20. case "openbsd":
  21. n, err = syscall.SysctlUint32("kern.somaxconn")
  22. }
  23. if n == 0 || err != nil {
  24. return syscall.SOMAXCONN
  25. }
  26. // FreeBSD stores the backlog in a uint16, as does Linux.
  27. // Assume the other BSDs do too. Truncate number to avoid wrapping.
  28. // See issue 5030.
  29. if n > 1<<16-1 {
  30. n = 1<<16 - 1
  31. }
  32. return int(n)
  33. }