try to connect through addrbook before requesting peers from seeds

we only use seeds if we can’t connect to peers in the addrbook.

Refs #864
This commit is contained in:
Anton Kaliaev 2017-12-27 14:16:49 -06:00
parent c521f385a6
commit 179d6062e4
No known key found for this signature in database
GPG Key ID: 7B6881D965918214

View File

@ -8,6 +8,7 @@ import (
"net" "net"
"net/http" "net/http"
"strings" "strings"
"time"
abci "github.com/tendermint/abci/types" abci "github.com/tendermint/abci/types"
crypto "github.com/tendermint/go-crypto" crypto "github.com/tendermint/go-crypto"
@ -379,19 +380,42 @@ func (n *Node) OnStart() error {
return err return err
} }
// If seeds exist, add them to the address book and dial out err = n.dialSeedsIfAddrBookIsEmptyOrPEXFailedToConnect()
if n.config.P2P.Seeds != "" { if err != nil {
// dial out return err
seeds := strings.Split(n.config.P2P.Seeds, ",")
if err := n.DialSeeds(seeds); err != nil {
return err
}
} }
// start tx indexer // start tx indexer
return n.indexerService.Start() 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. // OnStop stops the Node. It implements cmn.Service.
func (n *Node) OnStop() { func (n *Node) OnStop() {
n.BaseService.OnStop() n.BaseService.OnStop()