Skip to content

Commit 947c42f

Browse files
committed
chore: Update Go 1.23 TCP keep alive usage
1 parent 807c7a0 commit 947c42f

File tree

5 files changed

+108
-25
lines changed

5 files changed

+108
-25
lines changed

common/network/keep_alive_go122.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//go:build !go1.23
2+
3+
package network
4+
5+
import (
6+
"net"
7+
"time"
8+
)
9+
10+
// KeepAliveConfig is a copy of net.KeepAliveConfig backported from go1.23.
11+
type KeepAliveConfig struct {
12+
// If Enable is true, keep-alive probes are enabled.
13+
Enable bool
14+
15+
// Idle is the time that the connection must be idle before
16+
// the first keep-alive probe is sent.
17+
// If zero, a default value of 15 seconds is used.
18+
Idle time.Duration
19+
20+
// Interval is the time between keep-alive probes.
21+
// If zero, a default value of 15 seconds is used.
22+
Interval time.Duration
23+
24+
// Count is the maximum number of keep-alive probes that
25+
// can go unanswered before dropping a connection.
26+
// If zero, a default value of 9 is used.
27+
Count int
28+
}
29+
30+
func SetDialerTCPKeepAlive(dialer *net.Dialer, config KeepAliveConfig) {
31+
if config.Enable {
32+
dialer.KeepAlive = config.Idle
33+
}
34+
}
35+
36+
func SetListenerTCPKeepAlive(listenConfig *net.ListenConfig, config KeepAliveConfig) {
37+
if config.Enable {
38+
listenConfig.KeepAlive = config.Idle
39+
}
40+
}

common/network/keep_alive_go123.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//go:build go1.23
2+
3+
package network
4+
5+
import "net"
6+
7+
type KeepAliveConfig = net.KeepAliveConfig
8+
9+
func SetDialerTCPKeepAlive(dialer *net.Dialer, config KeepAliveConfig) {
10+
if config.Enable {
11+
dialer.KeepAliveConfig = config
12+
}
13+
}
14+
15+
func SetListenerTCPKeepAlive(listenConfig *net.ListenConfig, config KeepAliveConfig) {
16+
if config.Enable {
17+
listenConfig.KeepAliveConfig = config
18+
}
19+
}

common/network/sockopt.go

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,62 @@
11
package network
22

3-
import "github.com/layou233/zbproxy/v3/common/jsonx"
3+
import (
4+
"time"
5+
6+
"github.com/layou233/zbproxy/v3/common/jsonx"
7+
)
48

59
type InboundSocketOptions struct {
6-
KeepAlivePeriod jsonx.Duration `json:",omitempty"`
7-
Mark int `json:",omitempty"`
8-
SendThrough string `json:",omitempty"`
9-
TCPCongestion string `json:",omitempty"`
10-
TCPFastOpen bool `json:",omitempty"`
11-
MultiPathTCP bool `json:",omitempty"`
10+
keepAliveOptions
11+
Mark int `json:",omitempty"`
12+
SendThrough string `json:",omitempty"`
13+
TCPCongestion string `json:",omitempty"`
14+
TCPFastOpen bool `json:",omitempty"`
15+
MultiPathTCP bool `json:",omitempty"`
1216
}
1317

1418
type OutboundSocketOptions struct {
15-
KeepAlivePeriod jsonx.Duration `json:",omitempty"`
16-
Mark int `json:",omitempty"`
17-
Interface string `json:",omitempty"`
18-
SendThrough string `json:",omitempty"`
19-
TCPCongestion string `json:",omitempty"`
20-
TCPFastOpen bool `json:",omitempty"`
21-
MultiPathTCP bool `json:",omitempty"`
19+
keepAliveOptions
20+
Mark int `json:",omitempty"`
21+
Interface string `json:",omitempty"`
22+
SendThrough string `json:",omitempty"`
23+
TCPCongestion string `json:",omitempty"`
24+
TCPFastOpen bool `json:",omitempty"`
25+
MultiPathTCP bool `json:",omitempty"`
26+
}
27+
28+
type keepAliveOptions struct {
29+
KeepAliveIdle jsonx.Duration `json:",omitempty"`
30+
KeepAliveInterval jsonx.Duration `json:",omitempty"`
31+
KeepAliveCount int `json:",omitempty"`
32+
}
33+
34+
func (o *keepAliveOptions) KeepAliveConfig() (c KeepAliveConfig) {
35+
c = KeepAliveConfig{
36+
Idle: time.Duration(o.KeepAliveIdle),
37+
Interval: time.Duration(o.KeepAliveInterval),
38+
Count: o.KeepAliveCount,
39+
}
40+
if c.Idle > 0 || c.Interval > 0 || c.Count > 0 {
41+
c.Enable = true
42+
}
43+
return
2244
}
2345

2446
func ConvertLegacyOutboundOptions(inbound *InboundSocketOptions) *OutboundSocketOptions {
2547
if inbound == nil {
2648
return nil
2749
}
2850
return &OutboundSocketOptions{
29-
KeepAlivePeriod: inbound.KeepAlivePeriod,
30-
Mark: inbound.Mark,
31-
SendThrough: inbound.SendThrough,
32-
TCPCongestion: inbound.TCPCongestion,
33-
TCPFastOpen: inbound.TCPFastOpen,
34-
MultiPathTCP: inbound.MultiPathTCP,
51+
keepAliveOptions: keepAliveOptions{
52+
KeepAliveIdle: inbound.KeepAliveIdle,
53+
KeepAliveInterval: inbound.KeepAliveInterval,
54+
KeepAliveCount: inbound.KeepAliveCount,
55+
},
56+
Mark: inbound.Mark,
57+
SendThrough: inbound.SendThrough,
58+
TCPCongestion: inbound.TCPCongestion,
59+
TCPFastOpen: inbound.TCPFastOpen,
60+
MultiPathTCP: inbound.MultiPathTCP,
3561
}
3662
}

common/network/system.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"net"
66
"syscall"
7-
"time"
87
)
98

109
var SystemDialer Dialer = &systemOutbound{}
@@ -16,10 +15,10 @@ func NewSystemDialer(options *OutboundSocketOptions) Dialer {
1615

1716
out := &systemOutbound{
1817
Dialer: net.Dialer{
19-
Control: NewDialerControlFromOptions(options),
20-
KeepAlive: time.Duration(options.KeepAlivePeriod),
18+
Control: NewDialerControlFromOptions(options),
2119
},
2220
}
21+
SetDialerTCPKeepAlive(&out.Dialer, options.KeepAliveConfig())
2322
if options.SendThrough != "" {
2423
out.Dialer.LocalAddr = &net.TCPAddr{IP: net.ParseIP(options.SendThrough)}
2524
}

service/service.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"net/netip"
88
"os"
99
"strconv"
10-
"time"
1110

1211
"github.com/layou233/zbproxy/v3/adapter"
1312
"github.com/layou233/zbproxy/v3/common"
@@ -131,7 +130,7 @@ func (s *Service) Start(ctx context.Context) error {
131130
Control: network.NewListenerControlFromOptions(s.config.SocketOptions),
132131
}
133132
if s.config.SocketOptions != nil {
134-
listenConfig.KeepAlive = time.Duration(s.config.SocketOptions.KeepAlivePeriod)
133+
network.SetListenerTCPKeepAlive(listenConfig, s.config.SocketOptions.KeepAliveConfig())
135134
if s.config.SocketOptions.MultiPathTCP {
136135
network.SetListenerMultiPathTCP(listenConfig, true)
137136
}

0 commit comments

Comments
 (0)