//go:build linux || zos

package tcp

import (
	"fmt"
	"net"
	"time"

	"golang.org/x/sys/unix"
)

// SetUserTimeout sets the TCP user timeout on a connection's socket.
func SetUserTimeout(conn *net.TCPConn, timeout time.Duration) error {
	rawConn, err := conn.SyscallConn()
	if err != nil {
		return fmt.Errorf("Error getting raw connection: %w", err)
	}

	err = rawConn.Control(func(fd uintptr) {
		err = unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(timeout/time.Millisecond))
	})
	if err != nil {
		return fmt.Errorf("Error setting option on socket: %w", err)
	}

	return nil
}

//go:build linux || zos

package tcp

import (
	"fmt"
	"net"
	"time"

	"golang.org/x/sys/unix"
)

// SetUserTimeout sets the TCP user timeout on a connection's socket.
func SetUserTimeout(conn *net.TCPConn, timeout time.Duration) error {
	rawConn, err := conn.SyscallConn()
	if err != nil {
		return fmt.Errorf("Error getting raw connection: %w", err)
	}

	err = rawConn.Control(func(fd uintptr) {
		err = unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(timeout/time.Millisecond))
	})
	if err != nil {
		return fmt.Errorf("Error setting TCP_USER_TIMEOUT option on socket: %w", err)
	}

	return nil
}

Improve error message in SetUserTimeout
shared/tcp/tcp/timeout/user: Improve error message in SetUserTimeout

Signed-off-by: Thomas Parrott <6b778ce645fb0e3dde76d79eccad490955b1ae74@canonical.com>
