@@ -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.
1212type 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