chore: update tcp keepAlive setting for go1.23

This commit is contained in:
Larvan2
2024-08-14 11:51:39 +08:00
parent acaacd8ab1
commit 24c6e7d819
5 changed files with 38 additions and 9 deletions

View File

@@ -0,0 +1,12 @@
//go:build !go1.23
package net
import "net"
func TCPKeepAlive(c net.Conn) {
if tcp, ok := c.(*net.TCPConn); ok {
_ = tcp.SetKeepAlive(true)
_ = tcp.SetKeepAlivePeriod(KeepAliveInterval)
}
}

View File

@@ -0,0 +1,15 @@
//go:build go1.23
package net
import "net"
func TCPKeepAlive(c net.Conn) {
if tcp, ok := c.(*net.TCPConn); ok {
_ = tcp.SetKeepAliveConfig(net.KeepAliveConfig{
Enable: true,
Idle: KeepAliveIdle,
Interval: KeepAliveInterval,
})
}
}

View File

@@ -7,7 +7,10 @@ import (
"time"
)
var KeepAliveInterval = 15 * time.Second
var (
KeepAliveIdle = 0 * time.Second
KeepAliveInterval = 0 * time.Second
)
func SplitNetworkType(s string) (string, string, error) {
var (
@@ -47,10 +50,3 @@ func SplitHostPort(s string) (host, port string, hasPort bool, err error) {
host, port, err = net.SplitHostPort(temp)
return
}
func TCPKeepAlive(c net.Conn) {
if tcp, ok := c.(*net.TCPConn); ok {
_ = tcp.SetKeepAlive(true)
_ = tcp.SetKeepAlivePeriod(KeepAliveInterval)
}
}