tailscale/net/ktimeout/ktimeout.go
James Tucker 8fe504241d net/ktimeout: add a package to set TCP user timeout
Setting a user timeout will be a more practical tuning knob for a number
of endpoints, this provides a way to set it.

Updates tailscale/corp#17587

Signed-off-by: James Tucker <james@tailscale.com>
2024-02-20 10:49:58 -08:00

37 lines
1.2 KiB
Go

// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
// Package ktimeout configures kernel TCP stack timeouts via the provided
// control functions. Platform support varies; on unsupported platforms control
// functions may be entirely no-ops.
package ktimeout
import (
"fmt"
"syscall"
"time"
)
// UserTimeout returns a control function that sets the TCP user timeout
// (TCP_USER_TIMEOUT on linux). A user timeout specifies the maximum age of
// unacknowledged data on the connection (either in buffer, or sent but not
// acknowledged) before the connection is terminated. This timer has no effect
// on limiting the lifetime of idle connections. This may be entirely local to
// the network stack or may also apply RFC 5482 options to packets.
func UserTimeout(timeout time.Duration) func(network, address string, c syscall.RawConn) error {
return func(network, address string, c syscall.RawConn) error {
switch network {
case "tcp", "tcp4", "tcp6":
default:
return fmt.Errorf("ktimeout.UserTimeout: unsupported network: %s", network)
}
var err error
if e := c.Control(func(fd uintptr) {
err = SetUserTimeout(fd, timeout)
}); e != nil {
return e
}
return err
}
}