package dms3p2p import ( "context" "sort" "time" "gitlab.dms3.io/dms3/go-dms3/core/node/helpers" host "gitlab.dms3.io/p2p/go-p2p-core/host" routing "gitlab.dms3.io/p2p/go-p2p-core/routing" ddht "gitlab.dms3.io/p2p/go-p2p-kad-dht/dual" pubsub "gitlab.dms3.io/p2p/go-p2p-pubsub" namesys "gitlab.dms3.io/p2p/go-p2p-pubsub-router" record "gitlab.dms3.io/p2p/go-p2p-record" routinghelpers "gitlab.dms3.io/p2p/go-p2p-routing-helpers" "go.uber.org/fx" ) type BaseDms3Routing routing.Routing type Router struct { routing.Routing Priority int // less = more important } type p2pRouterOut struct { fx.Out Router Router `group:"routers"` } func BaseRouting(lc fx.Lifecycle, in BaseDms3Routing) (out p2pRouterOut, dr *ddht.DHT) { if dht, ok := in.(*ddht.DHT); ok { dr = dht lc.Append(fx.Hook{ OnStop: func(ctx context.Context) error { return dr.Close() }, }) } return p2pRouterOut{ Router: Router{ Priority: 1000, Routing: in, }, }, dr } type p2pOnlineRoutingIn struct { fx.In Routers []Router `group:"routers"` Validator record.Validator } func Routing(in p2pOnlineRoutingIn) routing.Routing { routers := in.Routers sort.SliceStable(routers, func(i, j int) bool { return routers[i].Priority < routers[j].Priority }) irouters := make([]routing.Routing, len(routers)) for i, v := range routers { irouters[i] = v.Routing } return routinghelpers.Tiered{ Routers: irouters, Validator: in.Validator, } } type p2pPSRoutingIn struct { fx.In BaseDms3Routing BaseDms3Routing Validator record.Validator Host host.Host PubSub *pubsub.PubSub `optional:"true"` } func PubsubRouter(mctx helpers.MetricsCtx, lc fx.Lifecycle, in p2pPSRoutingIn) (p2pRouterOut, *namesys.PubsubValueStore, error) { psRouter, err := namesys.NewPubsubValueStore( helpers.LifecycleCtx(mctx, lc), in.Host, in.PubSub, in.Validator, namesys.WithRebroadcastInterval(time.Minute), ) if err != nil { return p2pRouterOut{}, nil, err } return p2pRouterOut{ Router: Router{ Routing: &routinghelpers.Compose{ ValueStore: &routinghelpers.LimitedValueStore{ ValueStore: psRouter, Namespaces: []string{"dms3ns"}, }, }, Priority: 100, }, }, psRouter, nil }