lnd+rpcserver: allow customized timeout in ConnectPeer
This commit is contained in:
8 files changed
+754
-668
No files matched your search
@@ -68,6 +68,7 @@ import (
|
||||
"github.com/lightningnetwork/lnd/watchtower/wtclient"
|
||||
"github.com/lightningnetwork/lnd/watchtower/wtdb"
|
||||
"github.com/lightningnetwork/lnd/watchtower/wtpolicy"
|
||||
"github.com/lightningnetwork/lnd/watchtower/wtserver"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -325,11 +326,11 @@ func parseAddr(address string, netCfg tor.Net) (net.Addr, error) {
|
||||
// noiseDial is a factory function which creates a connmgr compliant dialing
|
||||
// function by returning a closure which includes the server's identity key.
|
||||
func noiseDial(idKey keychain.SingleKeyECDH,
|
||||
netCfg tor.Net) func(net.Addr) (net.Conn, error) {
|
||||
netCfg tor.Net, timeout time.Duration) func(net.Addr) (net.Conn, error) {
|
||||
|
||||
return func(a net.Addr) (net.Conn, error) {
|
||||
lnAddr := a.(*lnwire.NetAddress)
|
||||
return brontide.Dial(idKey, lnAddr, netCfg.Dial)
|
||||
return brontide.Dial(idKey, lnAddr, timeout, netCfg.Dial)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1234,12 +1235,23 @@ func newServer(cfg *Config, listenAddrs []net.Addr,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// authDial is the wrapper around the btrontide.Dial for the
|
||||
// watchtower.
|
||||
authDial := func(localKey keychain.SingleKeyECDH,
|
||||
netAddr *lnwire.NetAddress,
|
||||
dialer tor.DialFunc) (wtserver.Peer, error) {
|
||||
|
||||
return brontide.Dial(
|
||||
localKey, netAddr, cfg.ConnectionTimeout, dialer,
|
||||
)
|
||||
}
|
||||
|
||||
s.towerClient, err = wtclient.New(&wtclient.Config{
|
||||
Signer: cc.wallet.Cfg.Signer,
|
||||
NewAddress: newSweepPkScriptGen(cc.wallet),
|
||||
SecretKeyRing: s.cc.keyRing,
|
||||
Dial: cfg.net.Dial,
|
||||
AuthDial: wtclient.AuthDial,
|
||||
AuthDial: authDial,
|
||||
DB: towerClientDB,
|
||||
Policy: policy,
|
||||
ChainHash: *s.cfg.ActiveNetParams.GenesisHash,
|
||||
@@ -1330,8 +1342,10 @@ func newServer(cfg *Config, listenAddrs []net.Addr,
|
||||
OnAccept: s.InboundPeerConnected,
|
||||
RetryDuration: time.Second * 5,
|
||||
TargetOutbound: 100,
|
||||
Dial: noiseDial(s.identityECDH, s.cfg.net),
|
||||
OnConnection: s.OutboundPeerConnected,
|
||||
Dial: noiseDial(
|
||||
s.identityECDH, s.cfg.net, s.cfg.ConnectionTimeout,
|
||||
),
|
||||
OnConnection: s.OutboundPeerConnected,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -1845,7 +1859,7 @@ func initNetworkBootstrappers(s *server) ([]discovery.NetworkPeerBootstrapper, e
|
||||
"seeds: %v", dnsSeeds)
|
||||
|
||||
dnsBootStrapper := discovery.NewDNSSeedBootstrapper(
|
||||
dnsSeeds, s.cfg.net,
|
||||
dnsSeeds, s.cfg.net, s.cfg.ConnectionTimeout,
|
||||
)
|
||||
bootStrappers = append(bootStrappers, dnsBootStrapper)
|
||||
}
|
||||
@@ -1968,7 +1982,10 @@ func (s *server) peerBootstrapper(numTargetPeers uint32,
|
||||
// TODO(roasbeef): can do AS, subnet,
|
||||
// country diversity, etc
|
||||
errChan := make(chan error, 1)
|
||||
s.connectToPeer(a, errChan)
|
||||
s.connectToPeer(
|
||||
a, errChan,
|
||||
s.cfg.ConnectionTimeout,
|
||||
)
|
||||
select {
|
||||
case err := <-errChan:
|
||||
if err == nil {
|
||||
@@ -2070,7 +2087,9 @@ func (s *server) initialPeerBootstrap(ignore map[autopilot.NodeID]struct{},
|
||||
defer wg.Done()
|
||||
|
||||
errChan := make(chan error, 1)
|
||||
go s.connectToPeer(addr, errChan)
|
||||
go s.connectToPeer(
|
||||
addr, errChan, s.cfg.ConnectionTimeout,
|
||||
)
|
||||
|
||||
// We'll only allow this connection attempt to
|
||||
// take up to 3 seconds. This allows us to move
|
||||
@@ -3381,7 +3400,9 @@ type openChanReq struct {
|
||||
// connection is established, or the initial handshake process fails.
|
||||
//
|
||||
// NOTE: This function is safe for concurrent access.
|
||||
func (s *server) ConnectToPeer(addr *lnwire.NetAddress, perm bool) error {
|
||||
func (s *server) ConnectToPeer(addr *lnwire.NetAddress,
|
||||
perm bool, timeout time.Duration) error {
|
||||
|
||||
targetPub := string(addr.IdentityKey.SerializeCompressed())
|
||||
|
||||
// Acquire mutex, but use explicit unlocking instead of defer for
|
||||
@@ -3442,7 +3463,7 @@ func (s *server) ConnectToPeer(addr *lnwire.NetAddress, perm bool) error {
|
||||
// the crypto negotiation breaks down, then return an error to the
|
||||
// caller.
|
||||
errChan := make(chan error, 1)
|
||||
s.connectToPeer(addr, errChan)
|
||||
s.connectToPeer(addr, errChan, timeout)
|
||||
|
||||
select {
|
||||
case err := <-errChan:
|
||||
@@ -3455,8 +3476,12 @@ func (s *server) ConnectToPeer(addr *lnwire.NetAddress, perm bool) error {
|
||||
// connectToPeer establishes a connection to a remote peer. errChan is used to
|
||||
// notify the caller if the connection attempt has failed. Otherwise, it will be
|
||||
// closed.
|
||||
func (s *server) connectToPeer(addr *lnwire.NetAddress, errChan chan<- error) {
|
||||
conn, err := brontide.Dial(s.identityECDH, addr, s.cfg.net.Dial)
|
||||
func (s *server) connectToPeer(addr *lnwire.NetAddress,
|
||||
errChan chan<- error, timeout time.Duration) {
|
||||
|
||||
conn, err := brontide.Dial(
|
||||
s.identityECDH, addr, timeout, s.cfg.net.Dial,
|
||||
)
|
||||
if err != nil {
|
||||
srvrLog.Errorf("Unable to connect to %v: %v", addr, err)
|
||||
select {
|
||||
|
||||
Reference in new issue
Block a user