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