Skip to content

Commit 90d915e

Browse files
yichyayuhan6665
authored andcommitted
feat: add tcp_user_timeout
```json {"streamSettings":{"sockopt": {"tcpUserTimeout": 10000}}} ``` run `gofmt -w -s .` as well
1 parent d999453 commit 90d915e

File tree

5 files changed

+55
-35
lines changed

5 files changed

+55
-35
lines changed

common/xudp/xudp_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func TestXudpReadWrite(t *testing.T) {
1212
addr, _ := net.ParseDestination("tcp:127.0.0.1:1345")
1313
mb := make(buf.MultiBuffer, 0, 16)
14-
m := buf.MultiBufferContainer {
14+
m := buf.MultiBufferContainer{
1515
MultiBuffer: mb,
1616
}
1717
var arr [8]byte
@@ -33,4 +33,4 @@ func TestXudpReadWrite(t *testing.T) {
3333
if dest[0].UDP.Port != 1345 {
3434
t.Error("failed to parse xudp buffer")
3535
}
36-
}
36+
}

infra/conf/transport_internet.go

+2
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ type SocketConfig struct {
617617
TCPKeepAliveIdle int32 `json:"tcpKeepAliveIdle"`
618618
TCPCongestion string `json:"tcpCongestion"`
619619
TCPWindowClamp int32 `json:"tcpWindowClamp"`
620+
TCPUserTimeout int32 `json:"tcpUserTimeout"`
620621
V6only bool `json:"v6only"`
621622
Interface string `json:"interface"`
622623
}
@@ -669,6 +670,7 @@ func (c *SocketConfig) Build() (*internet.SocketConfig, error) {
669670
TcpKeepAliveIdle: c.TCPKeepAliveIdle,
670671
TcpCongestion: c.TCPCongestion,
671672
TcpWindowClamp: c.TCPWindowClamp,
673+
TcpUserTimeout: c.TCPUserTimeout,
672674
V6Only: c.V6only,
673675
Interface: c.Interface,
674676
}, nil

transport/internet/config.pb.go

+33-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transport/internet/config.proto

+2
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,6 @@ message SocketConfig {
104104
bool v6only = 14;
105105

106106
int32 tcp_window_clamp = 15;
107+
108+
int32 tcp_user_timeout = 16;
107109
}

transport/internet/sockopt_linux.go

+16-11
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ import (
77
"golang.org/x/sys/unix"
88
)
99

10-
const (
11-
// For incoming connections.
12-
TCP_FASTOPEN = 23
13-
// For out-going connections.
14-
TCP_FASTOPEN_CONNECT = 30
15-
)
16-
1710
func bindAddr(fd uintptr, ip []byte, port uint32) error {
1811
setReuseAddr(fd)
1912
setReusePort(fd)
@@ -59,8 +52,8 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf
5952
tfo = 1
6053
}
6154
if tfo >= 0 {
62-
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN_CONNECT, tfo); err != nil {
63-
return newError("failed to set TCP_FASTOPEN_CONNECT=", tfo).Base(err)
55+
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, unix.TCP_FASTOPEN_CONNECT, tfo); err != nil {
56+
return newError("failed to set TCP_FASTOPEN_CONNECT", tfo).Base(err)
6457
}
6558
}
6659

@@ -95,6 +88,12 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf
9588
return newError("failed to set TCP_WINDOW_CLAMP", err)
9689
}
9790
}
91+
92+
if config.TcpUserTimeout > 0 {
93+
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(config.TcpUserTimeout)); err != nil {
94+
return newError("failed to set TCP_USER_TIMEOUT", err)
95+
}
96+
}
9897
}
9998

10099
if config.Tproxy.IsEnabled() {
@@ -115,8 +114,8 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
115114
if isTCPSocket(network) {
116115
tfo := config.ParseTFOValue()
117116
if tfo >= 0 {
118-
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN, tfo); err != nil {
119-
return newError("failed to set TCP_FASTOPEN=", tfo).Base(err)
117+
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, unix.TCP_FASTOPEN, tfo); err != nil {
118+
return newError("failed to set TCP_FASTOPEN", tfo).Base(err)
120119
}
121120
}
122121

@@ -151,6 +150,12 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
151150
return newError("failed to set TCP_WINDOW_CLAMP", err)
152151
}
153152
}
153+
154+
if config.TcpUserTimeout > 0 {
155+
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(config.TcpUserTimeout)); err != nil {
156+
return newError("failed to set TCP_USER_TIMEOUT", err)
157+
}
158+
}
154159
}
155160

156161
if config.Tproxy.IsEnabled() {

0 commit comments

Comments
 (0)