Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ require (
golang.org/x/crypto v0.50.0
)

require golang.org/x/sys v0.43.0 // indirect
require golang.org/x/sys v0.43.0
36 changes: 36 additions & 0 deletions netx/tcpusertimeout_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//go:build linux

// Package netx holds network helpers that the standard net package doesn't have.
package netx

import (
"crypto/tls"
"net"
"time"

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

// SetTCPUserTimeout makes the kernel drop conn when sent data stays unacknowledged for timeout, instead of
// waiting for its ~15 min retry limit (e.g. when the peer's node is gone). Non-TCP connections are left alone.
func SetTCPUserTimeout(conn net.Conn, timeout time.Duration) error {
if tlsConn, ok := conn.(*tls.Conn); ok {
conn = tlsConn.NetConn()
}
tcpConn, ok := conn.(*net.TCPConn)
if !ok {
return nil
}
raw, err := tcpConn.SyscallConn()
if err != nil {
return err
}
var sockErr error
ctrlfn := func(fd uintptr) {
sockErr = unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(timeout.Milliseconds()))
}
if err := raw.Control(ctrlfn); err != nil {
return err
}
return sockErr
}
14 changes: 14 additions & 0 deletions netx/tcpusertimeout_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build !linux

// Package netx holds network helpers that the standard net package doesn't have.
package netx

import (
"net"
"time"
)

// SetTCPUserTimeout does nothing: TCP_USER_TIMEOUT is Linux only.
func SetTCPUserTimeout(net.Conn, time.Duration) error {
return nil
}
Loading