peer manager: Handle bootstrap in peermgr

This commit is contained in:
Łukasz Magiera 2019-10-23 13:11:18 +02:00
parent 1d1f468c98
commit 5175d8541e
4 changed files with 17 additions and 51 deletions

View File

@ -4,6 +4,7 @@ package main
import ( import (
"context" "context"
"github.com/filecoin-project/lotus/peermgr"
"io/ioutil" "io/ioutil"
"github.com/multiformats/go-multiaddr" "github.com/multiformats/go-multiaddr"
@ -95,8 +96,9 @@ var DaemonCmd = &cli.Command{
return lr.SetAPIEndpoint(apima) return lr.SetAPIEndpoint(apima)
}), }),
node.ApplyIf(func(s *node.Settings) bool { return cctx.Bool("bootstrap") }, node.ApplyIf(func(s *node.Settings) bool { return !cctx.Bool("bootstrap") },
node.Override(node.BootstrapKey, modules.Bootstrap), node.Unset(node.RunPeerMgrKey),
node.Unset(new(*peermgr.PeerMgr)),
), ),
) )
if err != nil { if err != nil {

View File

@ -15,6 +15,7 @@ import (
func main() { func main() {
logging.SetLogLevel("*", "INFO") logging.SetLogLevel("*", "INFO")
logging.SetLogLevel("dht", "ERROR")
local := []*cli.Command{ local := []*cli.Command{
DaemonCmd, DaemonCmd,
} }

View File

@ -3,24 +3,19 @@ package modules
import ( import (
"context" "context"
"crypto/rand" "crypto/rand"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/lib/addrutil" "github.com/filecoin-project/lotus/lib/addrutil"
"github.com/filecoin-project/lotus/node/modules/helpers" "github.com/filecoin-project/lotus/node/modules/dtypes"
"github.com/filecoin-project/lotus/node/repo"
"github.com/gbrlsnchs/jwt/v3" "github.com/gbrlsnchs/jwt/v3"
logging "github.com/ipfs/go-log" logging "github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peerstore" "github.com/libp2p/go-libp2p-core/peerstore"
record "github.com/libp2p/go-libp2p-record" record "github.com/libp2p/go-libp2p-record"
"go.uber.org/fx"
"golang.org/x/xerrors" "golang.org/x/xerrors"
"io" "io"
"io/ioutil" "io/ioutil"
"time"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/node/modules/dtypes"
"github.com/filecoin-project/lotus/node/repo"
) )
var log = logging.Logger("modules") var log = logging.Logger("modules")
@ -86,40 +81,3 @@ func ConfigBootstrap(peers []string) func() (dtypes.BootstrapPeers, error) {
func BuiltinBootstrap() (dtypes.BootstrapPeers, error) { func BuiltinBootstrap() (dtypes.BootstrapPeers, error) {
return build.BuiltinBootstrap() return build.BuiltinBootstrap()
} }
func Bootstrap(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host, pinfos dtypes.BootstrapPeers) {
ctx, cancel := context.WithCancel(mctx)
lc.Append(fx.Hook{
OnStart: func(_ context.Context) error {
go func() {
for {
sctx, cancel := context.WithTimeout(ctx, 2*time.Second)
<-sctx.Done()
cancel()
if ctx.Err() != nil {
return
}
if len(host.Network().Conns()) > 0 {
continue
}
log.Warn("No peers connected, performing automatic bootstrap")
for _, pi := range pinfos {
if err := host.Connect(ctx, pi); err != nil {
log.Warn("bootstrap connect failed: ", err)
}
}
}
}()
return nil
},
OnStop: func(_ context.Context) error {
cancel()
return nil
},
})
}

View File

@ -2,6 +2,7 @@ package peermgr
import ( import (
"context" "context"
"github.com/filecoin-project/lotus/node/modules/dtypes"
"sync" "sync"
"time" "time"
@ -41,12 +42,16 @@ type PeerMgr struct {
notifee *net.NotifyBundle notifee *net.NotifyBundle
} }
func NewPeerMgr(h host.Host, dht *dht.IpfsDHT) *PeerMgr { func NewPeerMgr(h host.Host, dht *dht.IpfsDHT, bootstrap dtypes.BootstrapPeers) *PeerMgr {
pm := &PeerMgr{ pm := &PeerMgr{
peers: make(map[peer.ID]struct{}), h: h,
dht: dht,
bootstrappers: bootstrap,
peers: make(map[peer.ID]struct{}),
maxFilPeers: MaxFilPeers, maxFilPeers: MaxFilPeers,
minFilPeers: MinFilPeers, minFilPeers: MinFilPeers,
h: h,
} }
pm.notifee = &net.NotifyBundle{ pm.notifee = &net.NotifyBundle{