sock_linux.go 648 B

1234567891011121314151617181920212223242526272829303132
  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. package net
  5. import "syscall"
  6. func maxListenerBacklog() int {
  7. fd, err := open("/proc/sys/net/core/somaxconn")
  8. if err != nil {
  9. return syscall.SOMAXCONN
  10. }
  11. defer fd.close()
  12. l, ok := fd.readLine()
  13. if !ok {
  14. return syscall.SOMAXCONN
  15. }
  16. f := getFields(l)
  17. n, _, ok := dtoi(f[0], 0)
  18. if n == 0 || !ok {
  19. return syscall.SOMAXCONN
  20. }
  21. // Linux stores the backlog in a uint16.
  22. // Truncate number to avoid wrapping.
  23. // See issue 5030.
  24. if n > 1<<16-1 {
  25. n = 1<<16 - 1
  26. }
  27. return n
  28. }