Skip to content

Commit bc004de

Browse files
Fanglidingmaoxikun
authored andcommitted
Inbounds & Outbounds: TCP KeepAlive better default value (XTLS#4931)
From XTLS#4927 (cherry picked from commit eb433d9)
1 parent 01fe414 commit bc004de

File tree

3 files changed

+46
-28
lines changed

3 files changed

+46
-28
lines changed

transport/internet/sockopt_linux.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,6 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf
6464
}
6565
}
6666

67-
if config.TcpKeepAliveInterval > 0 || config.TcpKeepAliveIdle > 0 {
68-
if config.TcpKeepAliveInterval > 0 {
69-
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil {
70-
return errors.New("failed to set TCP_KEEPINTVL", err)
71-
}
72-
}
73-
if config.TcpKeepAliveIdle > 0 {
74-
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPIDLE, int(config.TcpKeepAliveIdle)); err != nil {
75-
return errors.New("failed to set TCP_KEEPIDLE", err)
76-
}
77-
}
78-
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
79-
return errors.New("failed to set SO_KEEPALIVE", err)
80-
}
81-
} else if config.TcpKeepAliveInterval < 0 || config.TcpKeepAliveIdle < 0 {
82-
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 0); err != nil {
83-
return errors.New("failed to unset SO_KEEPALIVE", err)
84-
}
85-
}
86-
8767
if config.TcpCongestion != "" {
8868
if err := syscall.SetsockoptString(int(fd), syscall.SOL_TCP, syscall.TCP_CONGESTION, config.TcpCongestion); err != nil {
8969
return errors.New("failed to set TCP_CONGESTION", err)

transport/internet/system_dialer.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package internet
33
import (
44
"context"
55
"math/rand"
6+
gonet "net"
67
"syscall"
78
"time"
89

@@ -87,14 +88,34 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne
8788
Dest: destAddr,
8889
}, nil
8990
}
90-
goStdKeepAlive := time.Duration(0)
91-
if sockopt != nil && (sockopt.TcpKeepAliveInterval != 0 || sockopt.TcpKeepAliveIdle != 0) {
92-
goStdKeepAlive = time.Duration(-1)
91+
// Chrome defaults
92+
keepAliveConfig := gonet.KeepAliveConfig{
93+
Enable: true,
94+
Idle: 45 * time.Second,
95+
Interval: 45 * time.Second,
96+
Count: -1,
97+
}
98+
keepAlive := time.Duration(0)
99+
if sockopt != nil {
100+
if sockopt.TcpKeepAliveIdle*sockopt.TcpKeepAliveInterval < 0 {
101+
return nil, errors.New("invalid TcpKeepAliveIdle or TcpKeepAliveInterval value: ", sockopt.TcpKeepAliveIdle, " ", sockopt.TcpKeepAliveInterval)
102+
}
103+
if sockopt.TcpKeepAliveIdle < 0 || sockopt.TcpKeepAliveInterval < 0 {
104+
keepAlive = -1
105+
keepAliveConfig.Enable = false
106+
}
107+
if sockopt.TcpKeepAliveIdle > 0 {
108+
keepAliveConfig.Idle = time.Duration(sockopt.TcpKeepAliveIdle) * time.Second
109+
}
110+
if sockopt.TcpKeepAliveInterval > 0 {
111+
keepAliveConfig.Interval = time.Duration(sockopt.TcpKeepAliveInterval) * time.Second
112+
}
93113
}
94114
dialer := &net.Dialer{
95-
Timeout: time.Second * 16,
96-
LocalAddr: resolveSrcAddr(dest.Network, src),
97-
KeepAlive: goStdKeepAlive,
115+
Timeout: time.Second * 16,
116+
LocalAddr: resolveSrcAddr(dest.Network, src),
117+
KeepAlive: keepAlive,
118+
KeepAliveConfig: keepAliveConfig,
98119
}
99120

100121
if sockopt != nil || len(d.controllers) > 0 {

transport/internet/system_listener.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package internet
22

33
import (
44
"context"
5+
gonet "net"
56
"os"
67
"runtime"
78
"strconv"
@@ -88,9 +89,25 @@ func (dl *DefaultListener) Listen(ctx context.Context, addr net.Addr, sockopt *S
8889
network = addr.Network()
8990
address = addr.String()
9091
lc.Control = getControlFunc(ctx, sockopt, dl.controllers)
92+
// default disable keepalive
93+
lc.KeepAlive = -1
9194
if sockopt != nil {
92-
if sockopt.TcpKeepAliveInterval != 0 || sockopt.TcpKeepAliveIdle != 0 {
93-
lc.KeepAlive = time.Duration(-1)
95+
if sockopt.TcpKeepAliveIdle*sockopt.TcpKeepAliveInterval < 0 {
96+
return nil, errors.New("invalid TcpKeepAliveIdle or TcpKeepAliveInterval value: ", sockopt.TcpKeepAliveIdle, " ", sockopt.TcpKeepAliveInterval)
97+
}
98+
lc.KeepAliveConfig = gonet.KeepAliveConfig{
99+
Enable: false,
100+
Idle: -1,
101+
Interval: -1,
102+
Count: -1,
103+
}
104+
if sockopt.TcpKeepAliveIdle > 0 {
105+
lc.KeepAliveConfig.Enable = true
106+
lc.KeepAliveConfig.Idle = time.Duration(sockopt.TcpKeepAliveIdle) * time.Second
107+
}
108+
if sockopt.TcpKeepAliveInterval > 0 {
109+
lc.KeepAliveConfig.Enable = true
110+
lc.KeepAliveConfig.Interval = time.Duration(sockopt.TcpKeepAliveInterval) * time.Second
94111
}
95112
if sockopt.TcpMptcp {
96113
lc.SetMultipathTCP(true)

0 commit comments

Comments
 (0)