2019-07-02 12:40:25 +00:00
|
|
|
package lp2p
|
2019-07-01 09:09:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
"go.uber.org/fx"
|
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/helpers"
|
2019-07-01 09:09:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const discoveryConnTimeout = time.Second * 30
|
|
|
|
|
|
|
|
type discoveryHandler struct {
|
|
|
|
ctx context.Context
|
|
|
|
host host.Host
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dh *discoveryHandler) HandlePeerFound(p peer.AddrInfo) {
|
2019-07-03 11:55:52 +00:00
|
|
|
log.Warnw("discovred peer", "peer", p)
|
2019-07-01 09:09:48 +00:00
|
|
|
ctx, cancel := context.WithTimeout(dh.ctx, discoveryConnTimeout)
|
|
|
|
defer cancel()
|
|
|
|
if err := dh.host.Connect(ctx, p); err != nil {
|
2019-07-03 11:55:52 +00:00
|
|
|
log.Warnw("failed to connect to peer found by discovery", "error", err)
|
2019-07-01 09:09:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DiscoveryHandler(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host) *discoveryHandler {
|
|
|
|
return &discoveryHandler{
|
|
|
|
ctx: helpers.LifecycleCtx(mctx, lc),
|
|
|
|
host: host,
|
|
|
|
}
|
|
|
|
}
|