Skip to content

Commit f3d6751

Browse files
authored
Merge pull request #884 from apernet/fix-lazy
fix: lazy mode should defer config evaluation
2 parents b7dff17 + d73edff commit f3d6751

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

app/cmd/client.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -398,21 +398,19 @@ func runClient(cmd *cobra.Command, args []string) {
398398
if err := viper.Unmarshal(&config); err != nil {
399399
logger.Fatal("failed to parse client config", zap.Error(err))
400400
}
401-
hyConfig, err := config.Config()
402-
if err != nil {
403-
logger.Fatal("failed to load client config", zap.Error(err))
404-
}
405401

406-
c, err := client.NewReconnectableClient(hyConfig, func(c client.Client, info *client.HandshakeInfo, count int) {
407-
connectLog(info, count)
408-
// On the client side, we start checking for updates after we successfully connect
409-
// to the server, which, depending on whether lazy mode is enabled, may or may not
410-
// be immediately after the client starts. We don't want the update check request
411-
// to interfere with the lazy mode option.
412-
if count == 1 && !disableUpdateCheck {
413-
go runCheckUpdateClient(c)
414-
}
415-
}, config.Lazy)
402+
c, err := client.NewReconnectableClient(
403+
config.Config,
404+
func(c client.Client, info *client.HandshakeInfo, count int) {
405+
connectLog(info, count)
406+
// On the client side, we start checking for updates after we successfully connect
407+
// to the server, which, depending on whether lazy mode is enabled, may or may not
408+
// be immediately after the client starts. We don't want the update check request
409+
// to interfere with the lazy mode option.
410+
if count == 1 && !disableUpdateCheck {
411+
go runCheckUpdateClient(c)
412+
}
413+
}, config.Lazy)
416414
if err != nil {
417415
logger.Fatal("failed to initialize client", zap.Error(err))
418416
}

core/client/reconnect.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,21 @@ import (
1010
// reconnectableClientImpl is a wrapper of Client, which can reconnect when the connection is closed,
1111
// except when the caller explicitly calls Close() to permanently close this client.
1212
type reconnectableClientImpl struct {
13-
config *Config
13+
configFunc func() (*Config, error) // called before connecting
14+
connectedFunc func(Client, *HandshakeInfo, int) // called when successfully connected
1415
client Client
1516
count int
16-
connectedFunc func(Client, *HandshakeInfo, int) // called when successfully connected
1717
m sync.Mutex
1818
closed bool // permanent close
1919
}
2020

21-
func NewReconnectableClient(config *Config, connectedFunc func(Client, *HandshakeInfo, int), lazy bool) (Client, error) {
22-
// Make sure we capture any error in config and return it here,
23-
// so that the caller doesn't have to wait until the first call
24-
// to TCP() or UDP() to get the error (when lazy is true).
25-
if err := config.verifyAndFill(); err != nil {
26-
return nil, err
27-
}
21+
// NewReconnectableClient creates a reconnectable client.
22+
// If lazy is true, the client will not connect until the first call to TCP() or UDP().
23+
// We use a function for config mainly to delay config evaluation
24+
// (which involves DNS resolution) until the actual connection attempt.
25+
func NewReconnectableClient(configFunc func() (*Config, error), connectedFunc func(Client, *HandshakeInfo, int), lazy bool) (Client, error) {
2826
rc := &reconnectableClientImpl{
29-
config: config,
27+
configFunc: configFunc,
3028
connectedFunc: connectedFunc,
3129
}
3230
if !lazy {
@@ -41,9 +39,12 @@ func (rc *reconnectableClientImpl) reconnect() error {
4139
if rc.client != nil {
4240
_ = rc.client.Close()
4341
}
44-
var err error
4542
var info *HandshakeInfo
46-
rc.client, info, err = NewClient(rc.config)
43+
config, err := rc.configFunc()
44+
if err != nil {
45+
return err
46+
}
47+
rc.client, info, err = NewClient(config)
4748
if err != nil {
4849
return err
4950
} else {

0 commit comments

Comments
 (0)