Browse Source

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
pull/1030/head
Anton Kaliaev 7 years ago
parent
commit
179d6062e4
No known key found for this signature in database GPG Key ID: 7B6881D965918214
1 changed files with 31 additions and 7 deletions
  1. +31
    -7
      node/node.go

+ 31
- 7
node/node.go View File

@ -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()


Loading…
Cancel
Save