Propagate keep alive settings from the config

This commit is contained in:
9seconds
2026-04-07 13:41:44 +02:00
parent 83b43aecc1
commit 102f8a6cce
12 changed files with 76 additions and 24 deletions
+1 -1
View File
@@ -25,7 +25,7 @@ func (suite *BaseHTTPTestSuite) SetupSuite() {
}
func (suite *BaseHTTPTestSuite) SetupTest() {
suite.client = network.New(nil, "mtg/1", 0, 0, 0).MakeHTTPClient(nil)
suite.client = network.New(nil, "mtg/1", 0, 0, 0, network.DefaultKeepAliveConfig).MakeHTTPClient(nil)
}
func (suite *BaseHTTPTestSuite) TestGet() {
+1 -1
View File
@@ -19,7 +19,7 @@ type BaseNetworkTestSuite struct {
func (suite *BaseNetworkTestSuite) SetupSuite() {
suite.EchoServerTestSuite.SetupSuite()
suite.net = network.New(nil, "agent", 0, 0, 0)
suite.net = network.New(nil, "agent", 0, 0, 0, network.DefaultKeepAliveConfig)
}
func (suite *BaseNetworkTestSuite) TestDialUnknownNetwork() {
+22 -2
View File
@@ -11,6 +11,7 @@ package network
import (
"errors"
"net"
"time"
)
@@ -27,19 +28,25 @@ const (
// DefaultTCPKeepAlivePeriod defines a time period between 2 consecuitive
// probes.
//
// Deprecated: use DefaultKeepAliveIdle and DefaultKeepAliveInterval instead.
// Deprecated: use DefaultKeepAliveConfig
DefaultTCPKeepAlivePeriod = 10 * time.Second
// DefaultKeepAliveIdle is the time a connection must be idle before
// the first keepalive probe is sent.
//
// Deprecated: use DefaultKeepAliveConfig
DefaultKeepAliveIdle = 30 * time.Second
// DefaultKeepAliveInterval is the time between consecutive keepalive
// probes.
//
// Deprecated: use DefaultKeepAliveConfig
DefaultKeepAliveInterval = 10 * time.Second
// DefaultKeepAliveCount is the number of unacknowledged probes before
// the connection is considered dead.
//
// Deprecated: use DefaultKeepAliveConfig
DefaultKeepAliveCount = 3
// User Agent to use in HTTP client.
@@ -50,4 +57,17 @@ const (
tcpLingerTimeout = 1
)
var ErrCannotDial = errors.New("cannot dial to any address")
var (
ErrCannotDial = errors.New("cannot dial to any address")
// DefaultKeepAliveConfig defines a default configuration for
// keep alive settings. As per official documentation, if keep alive
// is enabled, then:
//
// Idle = 15 * time.Second
// Interval = 15 * time.Second
// Count = 9
DefaultKeepAliveConfig = net.KeepAliveConfig{
Enable: true,
}
)
+10 -7
View File
@@ -14,9 +14,10 @@ import (
type network struct {
net.Dialer
httpTimeout time.Duration
idleTimeout time.Duration
userAgent string
keepAliveConfig net.KeepAliveConfig
httpTimeout time.Duration
idleTimeout time.Duration
userAgent string
}
func (n *network) Dial(network, address string) (essentials.Conn, error) {
@@ -37,7 +38,7 @@ func (n *network) DialContext(ctx context.Context, network, address string) (ess
tcpConn := conn.(*net.TCPConn)
return tcpConn, setCommonSocketOptions(tcpConn)
return tcpConn, setCommonSocketOptions(tcpConn, n.keepAliveConfig)
}
func (n *network) MakeHTTPClient(
@@ -71,6 +72,7 @@ func New(
tcpTimeout,
httpTimeout,
idleTimeout time.Duration,
keepAliveConfig net.KeepAliveConfig,
) mtglib.Network {
if dnsResolver == nil {
dnsResolver = net.DefaultResolver
@@ -86,8 +88,9 @@ func New(
Resolver: dnsResolver,
FallbackDelay: -1,
},
userAgent: userAgent,
idleTimeout: idleTimeout,
httpTimeout: httpTimeout,
userAgent: userAgent,
idleTimeout: idleTimeout,
httpTimeout: httpTimeout,
keepAliveConfig: keepAliveConfig,
}
}
+2 -7
View File
@@ -5,13 +5,8 @@ import (
"net"
)
func setCommonSocketOptions(conn *net.TCPConn) error {
if err := conn.SetKeepAliveConfig(net.KeepAliveConfig{
Enable: true,
Idle: DefaultKeepAliveIdle,
Interval: DefaultKeepAliveInterval,
Count: DefaultKeepAliveCount,
}); err != nil {
func setCommonSocketOptions(conn *net.TCPConn, keepAliveConfig net.KeepAliveConfig) error {
if err := conn.SetKeepAliveConfig(keepAliveConfig); err != nil {
return fmt.Errorf("cannot configure TCP keepalive: %w", err)
}
+4 -4
View File
@@ -65,7 +65,7 @@ func TestSetCommonSocketOptionsKeepAlive(t *testing.T) {
tcpConn := accepted.(*net.TCPConn)
err = setCommonSocketOptions(tcpConn)
err = setCommonSocketOptions(tcpConn, DefaultKeepAliveConfig)
require.NoError(t, err)
rawConn, err := tcpConn.SyscallConn()
@@ -78,15 +78,15 @@ func TestSetCommonSocketOptionsKeepAlive(t *testing.T) {
idle, err := unix.GetsockoptInt(int(fd), syscall.IPPROTO_TCP, tcpKeepIdleOption())
require.NoError(t, err)
require.Equal(t, int(DefaultKeepAliveIdle.Seconds()), idle, "keepalive idle should match DefaultKeepAliveIdle")
require.Equal(t, 15, idle, "keepalive idle should match DefaultKeepAliveIdle")
interval, err := unix.GetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_KEEPINTVL)
require.NoError(t, err)
require.Equal(t, int(DefaultKeepAliveInterval.Seconds()), interval, "keepalive interval should match DefaultKeepAliveInterval")
require.Equal(t, 15, interval, "keepalive interval should match DefaultKeepAliveInterval")
count, err := unix.GetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_KEEPCNT)
require.NoError(t, err)
require.Equal(t, DefaultKeepAliveCount, count, "keepalive count should match DefaultKeepAliveCount")
require.Equal(t, 9, count, "keepalive count should match DefaultKeepAliveCount")
})
require.NoError(t, err)
}
+1 -1
View File
@@ -66,7 +66,7 @@ func (suite *SocksProxyTestSuite) SetupSuite() {
require.NoError(suite.T(), err)
suite.authURL = parsed
suite.baseNetwork = network.New(nil, "mtg", 0, 0, 0)
suite.baseNetwork = network.New(nil, "mtg", 0, 0, 0, network.DefaultKeepAliveConfig)
}
func (suite *SocksProxyTestSuite) TestIncorrectSchema() {