/ core / node / peering.go
peering.go
 1  package node
 2  
 3  import (
 4  	"context"
 5  
 6  	"github.com/ipfs/boxo/peering"
 7  	"github.com/libp2p/go-libp2p/core/host"
 8  	"github.com/libp2p/go-libp2p/core/peer"
 9  	"go.uber.org/fx"
10  )
11  
12  // Peering constructs the peering service and hooks it into fx's lifetime
13  // management system.
14  func Peering(lc fx.Lifecycle, host host.Host) *peering.PeeringService {
15  	ps := peering.NewPeeringService(host)
16  	lc.Append(fx.Hook{
17  		OnStart: func(context.Context) error {
18  			return ps.Start()
19  		},
20  		OnStop: func(context.Context) error {
21  			ps.Stop()
22  			return nil
23  		},
24  	})
25  	return ps
26  }
27  
28  // PeerWith configures the peering service to peer with the specified peers.
29  func PeerWith(peers ...peer.AddrInfo) fx.Option {
30  	return fx.Invoke(func(ps *peering.PeeringService) {
31  		for _, ai := range peers {
32  			ps.AddPeer(ai)
33  		}
34  	})
35  }