-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Update Go 1.23 TCP keep alive usage
- Loading branch information
Showing
5 changed files
with
108 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//go:build !go1.23 | ||
|
||
package network | ||
|
||
import ( | ||
"net" | ||
"time" | ||
) | ||
|
||
// KeepAliveConfig is a copy of net.KeepAliveConfig backported from go1.23. | ||
type KeepAliveConfig struct { | ||
// If Enable is true, keep-alive probes are enabled. | ||
Enable bool | ||
|
||
// Idle is the time that the connection must be idle before | ||
// the first keep-alive probe is sent. | ||
// If zero, a default value of 15 seconds is used. | ||
Idle time.Duration | ||
|
||
// Interval is the time between keep-alive probes. | ||
// If zero, a default value of 15 seconds is used. | ||
Interval time.Duration | ||
|
||
// Count is the maximum number of keep-alive probes that | ||
// can go unanswered before dropping a connection. | ||
// If zero, a default value of 9 is used. | ||
Count int | ||
} | ||
|
||
func SetDialerTCPKeepAlive(dialer *net.Dialer, config KeepAliveConfig) { | ||
if config.Enable { | ||
dialer.KeepAlive = config.Idle | ||
} | ||
} | ||
|
||
func SetListenerTCPKeepAlive(listenConfig *net.ListenConfig, config KeepAliveConfig) { | ||
if config.Enable { | ||
listenConfig.KeepAlive = config.Idle | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//go:build go1.23 | ||
|
||
package network | ||
|
||
import "net" | ||
|
||
type KeepAliveConfig = net.KeepAliveConfig | ||
|
||
func SetDialerTCPKeepAlive(dialer *net.Dialer, config KeepAliveConfig) { | ||
if config.Enable { | ||
dialer.KeepAliveConfig = config | ||
} | ||
} | ||
|
||
func SetListenerTCPKeepAlive(listenConfig *net.ListenConfig, config KeepAliveConfig) { | ||
if config.Enable { | ||
listenConfig.KeepAliveConfig = config | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,62 @@ | ||
package network | ||
|
||
import "github.com/layou233/zbproxy/v3/common/jsonx" | ||
import ( | ||
"time" | ||
|
||
"github.com/layou233/zbproxy/v3/common/jsonx" | ||
) | ||
|
||
type InboundSocketOptions struct { | ||
KeepAlivePeriod jsonx.Duration `json:",omitempty"` | ||
Mark int `json:",omitempty"` | ||
SendThrough string `json:",omitempty"` | ||
TCPCongestion string `json:",omitempty"` | ||
TCPFastOpen bool `json:",omitempty"` | ||
MultiPathTCP bool `json:",omitempty"` | ||
keepAliveOptions | ||
Mark int `json:",omitempty"` | ||
SendThrough string `json:",omitempty"` | ||
TCPCongestion string `json:",omitempty"` | ||
TCPFastOpen bool `json:",omitempty"` | ||
MultiPathTCP bool `json:",omitempty"` | ||
} | ||
|
||
type OutboundSocketOptions struct { | ||
KeepAlivePeriod jsonx.Duration `json:",omitempty"` | ||
Mark int `json:",omitempty"` | ||
Interface string `json:",omitempty"` | ||
SendThrough string `json:",omitempty"` | ||
TCPCongestion string `json:",omitempty"` | ||
TCPFastOpen bool `json:",omitempty"` | ||
MultiPathTCP bool `json:",omitempty"` | ||
keepAliveOptions | ||
Mark int `json:",omitempty"` | ||
Interface string `json:",omitempty"` | ||
SendThrough string `json:",omitempty"` | ||
TCPCongestion string `json:",omitempty"` | ||
TCPFastOpen bool `json:",omitempty"` | ||
MultiPathTCP bool `json:",omitempty"` | ||
} | ||
|
||
type keepAliveOptions struct { | ||
KeepAliveIdle jsonx.Duration `json:",omitempty"` | ||
KeepAliveInterval jsonx.Duration `json:",omitempty"` | ||
KeepAliveCount int `json:",omitempty"` | ||
} | ||
|
||
func (o *keepAliveOptions) KeepAliveConfig() (c KeepAliveConfig) { | ||
c = KeepAliveConfig{ | ||
Idle: time.Duration(o.KeepAliveIdle), | ||
Interval: time.Duration(o.KeepAliveInterval), | ||
Count: o.KeepAliveCount, | ||
} | ||
if c.Idle > 0 || c.Interval > 0 || c.Count > 0 { | ||
c.Enable = true | ||
} | ||
return | ||
} | ||
|
||
func ConvertLegacyOutboundOptions(inbound *InboundSocketOptions) *OutboundSocketOptions { | ||
if inbound == nil { | ||
return nil | ||
} | ||
return &OutboundSocketOptions{ | ||
KeepAlivePeriod: inbound.KeepAlivePeriod, | ||
Mark: inbound.Mark, | ||
SendThrough: inbound.SendThrough, | ||
TCPCongestion: inbound.TCPCongestion, | ||
TCPFastOpen: inbound.TCPFastOpen, | ||
MultiPathTCP: inbound.MultiPathTCP, | ||
keepAliveOptions: keepAliveOptions{ | ||
KeepAliveIdle: inbound.KeepAliveIdle, | ||
KeepAliveInterval: inbound.KeepAliveInterval, | ||
KeepAliveCount: inbound.KeepAliveCount, | ||
}, | ||
Mark: inbound.Mark, | ||
SendThrough: inbound.SendThrough, | ||
TCPCongestion: inbound.TCPCongestion, | ||
TCPFastOpen: inbound.TCPFastOpen, | ||
MultiPathTCP: inbound.MultiPathTCP, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters