peer manager: Handle bootstrap in peermgr
This commit is contained in:
parent
1d1f468c98
commit
5175d8541e
@ -4,6 +4,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/filecoin-project/lotus/peermgr"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/multiformats/go-multiaddr"
|
||||
@ -95,8 +96,9 @@ var DaemonCmd = &cli.Command{
|
||||
return lr.SetAPIEndpoint(apima)
|
||||
}),
|
||||
|
||||
node.ApplyIf(func(s *node.Settings) bool { return cctx.Bool("bootstrap") },
|
||||
node.Override(node.BootstrapKey, modules.Bootstrap),
|
||||
node.ApplyIf(func(s *node.Settings) bool { return !cctx.Bool("bootstrap") },
|
||||
node.Unset(node.RunPeerMgrKey),
|
||||
node.Unset(new(*peermgr.PeerMgr)),
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
|
@ -15,6 +15,7 @@ import (
|
||||
|
||||
func main() {
|
||||
logging.SetLogLevel("*", "INFO")
|
||||
logging.SetLogLevel("dht", "ERROR")
|
||||
local := []*cli.Command{
|
||||
DaemonCmd,
|
||||
}
|
||||
|
@ -3,24 +3,19 @@ package modules
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"github.com/filecoin-project/lotus/api"
|
||||
"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/node/modules/helpers"
|
||||
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
||||
"github.com/filecoin-project/lotus/node/repo"
|
||||
"github.com/gbrlsnchs/jwt/v3"
|
||||
logging "github.com/ipfs/go-log"
|
||||
"github.com/libp2p/go-libp2p-core/host"
|
||||
"github.com/libp2p/go-libp2p-core/peerstore"
|
||||
record "github.com/libp2p/go-libp2p-record"
|
||||
"go.uber.org/fx"
|
||||
"golang.org/x/xerrors"
|
||||
"io"
|
||||
"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")
|
||||
@ -86,40 +81,3 @@ func ConfigBootstrap(peers []string) func() (dtypes.BootstrapPeers, error) {
|
||||
func BuiltinBootstrap() (dtypes.BootstrapPeers, error) {
|
||||
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
|
||||
},
|
||||
})
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package peermgr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@ -41,12 +42,16 @@ type PeerMgr struct {
|
||||
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{
|
||||
peers: make(map[peer.ID]struct{}),
|
||||
h: h,
|
||||
dht: dht,
|
||||
bootstrappers: bootstrap,
|
||||
|
||||
peers: make(map[peer.ID]struct{}),
|
||||
|
||||
maxFilPeers: MaxFilPeers,
|
||||
minFilPeers: MinFilPeers,
|
||||
h: h,
|
||||
}
|
||||
|
||||
pm.notifee = &net.NotifyBundle{
|
||||
|
Loading…
Reference in New Issue
Block a user