Commit bf380b87 authored by Łukasz Magiera's avatar Łukasz Magiera Committed by Steven Allen

Cleanup routing related units

License: MIT
Signed-off-by: default avatarŁukasz Magiera <magik6k@gmail.com>
parent 23f50ab0
...@@ -35,15 +35,19 @@ var BaseLibP2P = fx.Options( ...@@ -35,15 +35,19 @@ var BaseLibP2P = fx.Options(
) )
func LibP2P(cfg *BuildCfg) fx.Option { func LibP2P(cfg *BuildCfg) fx.Option {
return fx.Options( opts := fx.Options(
BaseLibP2P, BaseLibP2P,
maybeProvide(P2PNoSecurity, cfg.DisableEncryptedConnections), maybeProvide(P2PNoSecurity, cfg.DisableEncryptedConnections),
maybeProvide(Pubsub, cfg.getOpt("pubsub") || cfg.getOpt("ipnsps")), maybeProvide(Pubsub, cfg.getOpt("pubsub") || cfg.getOpt("ipnsps")),
fx.Provide(P2PSmuxTransport(cfg.getOpt("mplex"))), fx.Provide(P2PSmuxTransport(cfg.getOpt("mplex"))),
fx.Provide(P2POnlineRouting(cfg.getOpt("ipnsps"))), fx.Provide(P2PRouting),
fx.Provide(P2PBaseRouting),
maybeProvide(P2PPubsubRouter, cfg.getOpt("ipnsps")),
) )
return opts
} }
func Storage(cfg *BuildCfg) fx.Option { func Storage(cfg *BuildCfg) fx.Option {
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"sort"
"strings" "strings"
"time" "time"
...@@ -398,10 +399,8 @@ type P2PHostOut struct { ...@@ -398,10 +399,8 @@ type P2PHostOut struct {
Host host.Host Host host.Host
Routing BaseRouting Routing BaseRouting
IpfsDHT *dht.IpfsDHT
} }
// TODO: move some of this into params struct
func P2PHost(mctx MetricsCtx, lc fx.Lifecycle, params P2PHostIn) (out P2PHostOut, err error) { func P2PHost(mctx MetricsCtx, lc fx.Lifecycle, params P2PHostIn) (out P2PHostOut, err error) {
opts := []libp2p.Option{libp2p.NoListenAddrs} opts := []libp2p.Option{libp2p.NoListenAddrs}
for _, o := range params.Opts { for _, o := range params.Opts {
...@@ -438,80 +437,95 @@ func P2PHost(mctx MetricsCtx, lc fx.Lifecycle, params P2PHostIn) (out P2PHostOut ...@@ -438,80 +437,95 @@ func P2PHost(mctx MetricsCtx, lc fx.Lifecycle, params P2PHostIn) (out P2PHostOut
}, },
}) })
// TODO: break this up into more DI units return out, err
// TODO: I'm not a fan of type assertions like this but the }
// `RoutingOption` system doesn't currently provide access to the
// IpfsNode. type Router struct {
// routing.IpfsRouting
// Ideally, we'd do something like:
// Priority int // less = more important
// 1. Add some fancy method to introspect into tiered routers to extract }
// things like the pubsub router or the DHT (complicated, messy,
// probably not worth it). type p2pRouterOut struct {
// 2. Pass the IpfsNode into the RoutingOption (would also remove the fx.Out
// PSRouter case below.
// 3. Introduce some kind of service manager? (my personal favorite but Router Router `group:"routers"`
// that requires a fair amount of work). }
if dht, ok := out.Routing.(*dht.IpfsDHT); ok {
out.IpfsDHT = dht func P2PBaseRouting(lc fx.Lifecycle, in BaseRouting) (out p2pRouterOut, dr *dht.IpfsDHT) {
if dht, ok := in.(*dht.IpfsDHT); ok {
dr = dht
lc.Append(fx.Hook{ lc.Append(fx.Hook{
OnStop: func(ctx context.Context) error { OnStop: func(ctx context.Context) error {
return out.IpfsDHT.Close() return dr.Close()
}, },
}) })
} }
return out, err return p2pRouterOut{
Router: Router{
Priority: 1000,
IpfsRouting: in,
},
}, dr
} }
type p2pRoutingIn struct { type p2pOnlineRoutingIn struct {
fx.In fx.In
Repo repo.Repo Routers []Router `group:"routers"`
Validator record.Validator Validator record.Validator
Host host.Host }
PubSub *pubsub.PubSub `optional:"true"`
BaseRouting BaseRouting func P2PRouting(in p2pOnlineRoutingIn) routing.IpfsRouting {
routers := in.Routers
sort.SliceStable(routers, func(i, j int) bool {
return routers[i].Priority < routers[j].Priority
})
irouters := make([]routing.IpfsRouting, len(routers))
for i, v := range routers {
irouters[i] = v.IpfsRouting
}
return routinghelpers.Tiered{
Routers: irouters,
Validator: in.Validator,
}
} }
type p2pRoutingOut struct { type p2pPSRoutingIn struct {
fx.Out fx.In
IpfsRouting routing.IpfsRouting BaseRouting BaseRouting
PSRouter *namesys.PubsubValueStore Repo repo.Repo
} Validator record.Validator
Host host.Host
func P2POnlineRouting(ipnsps bool) func(mctx MetricsCtx, lc fx.Lifecycle, in p2pRoutingIn) (out p2pRoutingOut) { PubSub *pubsub.PubSub `optional:"true"`
return func(mctx MetricsCtx, lc fx.Lifecycle, in p2pRoutingIn) (out p2pRoutingOut) { }
out.IpfsRouting = in.BaseRouting
func P2PPubsubRouter(mctx MetricsCtx, lc fx.Lifecycle, in p2pPSRoutingIn) (p2pRouterOut, *namesys.PubsubValueStore) {
if ipnsps { psRouter := namesys.NewPubsubValueStore(
out.PSRouter = namesys.NewPubsubValueStore( lifecycleCtx(mctx, lc),
lifecycleCtx(mctx, lc), in.Host,
in.Host, in.BaseRouting,
in.BaseRouting, in.PubSub,
in.PubSub, in.Validator,
in.Validator, )
)
return p2pRouterOut{
out.IpfsRouting = routinghelpers.Tiered{ Router: Router{
Routers: []routing.IpfsRouting{ IpfsRouting: &routinghelpers.Compose{
// Always check pubsub first. ValueStore: &routinghelpers.LimitedValueStore{
&routinghelpers.Compose{ ValueStore: psRouter,
ValueStore: &routinghelpers.LimitedValueStore{ Namespaces: []string{"ipns"},
ValueStore: out.PSRouter,
Namespaces: []string{"ipns"},
},
},
in.BaseRouting,
}, },
Validator: in.Validator, },
} Priority: 100,
} },
return out }, psRouter
}
} }
func AutoNATService(mctx MetricsCtx, lc fx.Lifecycle, cfg *config.Config, host host.Host) error { func AutoNATService(mctx MetricsCtx, lc fx.Lifecycle, cfg *config.Config, host host.Host) error {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment