mirror of
https://github.com/cedar2025/hysteria.git
synced 2025-08-15 13:51:49 +00:00
fix: lazy mode should defer config evaluation
This commit is contained in:
@@ -10,23 +10,21 @@ import (
|
||||
// reconnectableClientImpl is a wrapper of Client, which can reconnect when the connection is closed,
|
||||
// except when the caller explicitly calls Close() to permanently close this client.
|
||||
type reconnectableClientImpl struct {
|
||||
config *Config
|
||||
configFunc func() (*Config, error) // called before connecting
|
||||
connectedFunc func(Client, *HandshakeInfo, int) // called when successfully connected
|
||||
client Client
|
||||
count int
|
||||
connectedFunc func(Client, *HandshakeInfo, int) // called when successfully connected
|
||||
m sync.Mutex
|
||||
closed bool // permanent close
|
||||
}
|
||||
|
||||
func NewReconnectableClient(config *Config, connectedFunc func(Client, *HandshakeInfo, int), lazy bool) (Client, error) {
|
||||
// Make sure we capture any error in config and return it here,
|
||||
// so that the caller doesn't have to wait until the first call
|
||||
// to TCP() or UDP() to get the error (when lazy is true).
|
||||
if err := config.verifyAndFill(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// NewReconnectableClient creates a reconnectable client.
|
||||
// If lazy is true, the client will not connect until the first call to TCP() or UDP().
|
||||
// We use a function for config mainly to delay config evaluation
|
||||
// (which involves DNS resolution) until the actual connection attempt.
|
||||
func NewReconnectableClient(configFunc func() (*Config, error), connectedFunc func(Client, *HandshakeInfo, int), lazy bool) (Client, error) {
|
||||
rc := &reconnectableClientImpl{
|
||||
config: config,
|
||||
configFunc: configFunc,
|
||||
connectedFunc: connectedFunc,
|
||||
}
|
||||
if !lazy {
|
||||
@@ -41,9 +39,12 @@ func (rc *reconnectableClientImpl) reconnect() error {
|
||||
if rc.client != nil {
|
||||
_ = rc.client.Close()
|
||||
}
|
||||
var err error
|
||||
var info *HandshakeInfo
|
||||
rc.client, info, err = NewClient(rc.config)
|
||||
config, err := rc.configFunc()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rc.client, info, err = NewClient(config)
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user