From 179d6062e4c385b67af414129d6c3cd9b09cb79a Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Wed, 27 Dec 2017 14:16:49 -0600 Subject: [PATCH] try to connect through addrbook before requesting peers from seeds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit we only use seeds if we can’t connect to peers in the addrbook. Refs #864 --- node/node.go | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/node/node.go b/node/node.go index fde8e1e0a..f8164955d 100644 --- a/node/node.go +++ b/node/node.go @@ -8,6 +8,7 @@ import ( "net" "net/http" "strings" + "time" abci "github.com/tendermint/abci/types" crypto "github.com/tendermint/go-crypto" @@ -379,19 +380,42 @@ func (n *Node) OnStart() error { return err } - // If seeds exist, add them to the address book and dial out - if n.config.P2P.Seeds != "" { - // dial out - seeds := strings.Split(n.config.P2P.Seeds, ",") - if err := n.DialSeeds(seeds); err != nil { - return err - } + err = n.dialSeedsIfAddrBookIsEmptyOrPEXFailedToConnect() + if err != nil { + return err } // start tx indexer return n.indexerService.Start() } +func (n *Node) dialSeedsIfAddrBookIsEmptyOrPEXFailedToConnect() error { + if n.config.P2P.Seeds == "" { + return nil + } + + seeds := strings.Split(n.config.P2P.Seeds, ",") + + // prefer peers from address book + if n.config.P2P.PexReactor && n.addrBook.Size() > 0 { + // give some time to PexReactor to connect us to other peers + const fallbackToSeedsAfterSec = 30 * time.Second + go func() { + time.Sleep(fallbackToSeedsAfterSec) + // fallback to dialing seeds if for some reason we can't connect to any + // peers + outbound, inbound, _ := n.sw.NumPeers() + if n.IsRunning() && outbound+inbound == 0 { + n.DialSeeds(seeds) + } + }() + return nil + } + + // add seeds to the address book and dial out + return n.DialSeeds(seeds) +} + // OnStop stops the Node. It implements cmn.Service. func (n *Node) OnStop() { n.BaseService.OnStop()