ktimeout.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Package ktimeout configures kernel TCP stack timeouts via the provided
  4. // control functions. Platform support varies; on unsupported platforms control
  5. // functions may be entirely no-ops.
  6. package ktimeout
  7. import (
  8. "fmt"
  9. "syscall"
  10. "time"
  11. )
  12. // UserTimeout returns a control function that sets the TCP user timeout
  13. // (TCP_USER_TIMEOUT on linux). A user timeout specifies the maximum age of
  14. // unacknowledged data on the connection (either in buffer, or sent but not
  15. // acknowledged) before the connection is terminated. This timer has no effect
  16. // on limiting the lifetime of idle connections. This may be entirely local to
  17. // the network stack or may also apply RFC 5482 options to packets.
  18. func UserTimeout(timeout time.Duration) func(network, address string, c syscall.RawConn) error {
  19. return func(network, address string, c syscall.RawConn) error {
  20. switch network {
  21. case "tcp", "tcp4", "tcp6":
  22. default:
  23. return fmt.Errorf("ktimeout.UserTimeout: unsupported network: %s", network)
  24. }
  25. var err error
  26. if e := c.Control(func(fd uintptr) {
  27. err = SetUserTimeout(fd, timeout)
  28. }); e != nil {
  29. return e
  30. }
  31. return err
  32. }
  33. }