mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 13:04:02 +03:00
Merge pull request #453 from 9seconds/tcp-user-timeout
Add TCP_USER_TIMEOUT support
This commit is contained in:
@@ -24,6 +24,7 @@ func setCommonSocketOptions(conn *net.TCPConn, keepAliveConfig net.KeepAliveConf
|
|||||||
}
|
}
|
||||||
|
|
||||||
setCongestionControl(rawConn)
|
setCongestionControl(rawConn)
|
||||||
|
setTCPUserTimeout(rawConn, keepAliveConfig)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,11 +3,50 @@
|
|||||||
package network
|
package network
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Go runtime defaults for KeepAliveConfig when fields are zero.
|
||||||
|
const (
|
||||||
|
goDefaultKeepAliveIdle = 15 * time.Second
|
||||||
|
goDefaultKeepAliveInterval = 15 * time.Second
|
||||||
|
goDefaultKeepAliveCount = 9
|
||||||
|
)
|
||||||
|
|
||||||
|
// setTCPUserTimeout sets TCP_USER_TIMEOUT on a socket. If transmitted
|
||||||
|
// data remains unacknowledged for this long, the kernel closes the
|
||||||
|
// connection. As recommended by Cloudflare
|
||||||
|
// (https://blog.cloudflare.com/when-tcp-sockets-refuse-to-die/),
|
||||||
|
// the value is computed as: keepidle + keepintvl * keepcnt. This
|
||||||
|
// ensures TCP_USER_TIMEOUT and keepalives agree on when to give up.
|
||||||
|
// Best-effort: silently ignored if unsupported.
|
||||||
|
func setTCPUserTimeout(conn syscall.RawConn, cfg net.KeepAliveConfig) {
|
||||||
|
idle := cfg.Idle
|
||||||
|
if idle == 0 {
|
||||||
|
idle = goDefaultKeepAliveIdle
|
||||||
|
}
|
||||||
|
|
||||||
|
interval := cfg.Interval
|
||||||
|
if interval == 0 {
|
||||||
|
interval = goDefaultKeepAliveInterval
|
||||||
|
}
|
||||||
|
|
||||||
|
count := cfg.Count
|
||||||
|
if count == 0 {
|
||||||
|
count = goDefaultKeepAliveCount
|
||||||
|
}
|
||||||
|
|
||||||
|
timeout := idle + interval*time.Duration(count)
|
||||||
|
|
||||||
|
conn.Control(func(fd uintptr) { //nolint: errcheck
|
||||||
|
unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(timeout.Milliseconds())) //nolint: errcheck
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func setCongestionControl(conn syscall.RawConn) {
|
func setCongestionControl(conn syscall.RawConn) {
|
||||||
conn.Control(func(fd uintptr) { //nolint: errcheck
|
conn.Control(func(fd uintptr) { //nolint: errcheck
|
||||||
// BBR provides better throughput over lossy and high-latency links compared
|
// BBR provides better throughput over lossy and high-latency links compared
|
||||||
|
|||||||
@@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
package network
|
package network
|
||||||
|
|
||||||
import "syscall"
|
import (
|
||||||
|
"net"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
func setCongestionControl(conn syscall.RawConn) {}
|
func setCongestionControl(conn syscall.RawConn) {}
|
||||||
|
func setTCPUserTimeout(conn syscall.RawConn, cfg net.KeepAliveConfig) {}
|
||||||
|
|||||||
Reference in New Issue
Block a user