From 8fd91c4b0ae43c1b8c88f8c4e2519fca590f0f29 Mon Sep 17 00:00:00 2001 From: Antonio Navarro Perez Date: Tue, 13 Sep 2022 12:05:43 +0200 Subject: [PATCH] Add ProvideMany Signed-off-by: Antonio Navarro Perez --- compconfig.go | 7 +++++++ compparallel.go | 32 ++++++++++++++++++++++++++++++++ compsequential.go | 38 +++++++++++++++++++++++++++++++++++--- 3 files changed, 74 insertions(+), 3 deletions(-) diff --git a/compconfig.go b/compconfig.go index bcb1d07..7618830 100644 --- a/compconfig.go +++ b/compconfig.go @@ -1,9 +1,11 @@ package routinghelpers import ( + "context" "time" "github.com/libp2p/go-libp2p-core/routing" + "github.com/multiformats/go-multihash" ) type ParallelRouter struct { @@ -18,3 +20,8 @@ type SequentialRouter struct { IgnoreError bool Router routing.Routing } + +type ProvideManyRouter interface { + ProvideMany(ctx context.Context, keys []multihash.Multihash) error + Ready() bool +} diff --git a/compparallel.go b/compparallel.go index bbc5c0a..c0684b3 100644 --- a/compparallel.go +++ b/compparallel.go @@ -11,9 +11,11 @@ import ( "github.com/ipfs/go-cid" "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/routing" + "github.com/multiformats/go-multihash" ) var _ routing.Routing = &composableParallel{} +var _ ProvideManyRouter = &composableParallel{} type composableParallel struct { routers []*ParallelRouter @@ -38,6 +40,36 @@ func (r *composableParallel) Provide(ctx context.Context, cid cid.Cid, provide b ) } +// ProvideMany will call all supported Routers in parallel. +func (r *composableParallel) ProvideMany(ctx context.Context, keys []multihash.Multihash) error { + return executeParallel(ctx, r.routers, + func(ctx context.Context, r routing.Routing) error { + pm, ok := r.(ProvideManyRouter) + if !ok { + return nil + } + return pm.ProvideMany(ctx, keys) + }, + ) +} + +// Ready will call all supported ProvideMany Routers SEQUENTIALLY. +// If some of them are not ready, this method will return false. +func (r *composableParallel) Ready() bool { + for _, ro := range r.routers { + pm, ok := ro.Router.(ProvideManyRouter) + if !ok { + continue + } + + if !pm.Ready() { + return false + } + } + + return true +} + // FindProvidersAsync will execute all Routers in parallel, iterating results from them in unspecified order. // If count is set, only that amount of elements will be returned without any specification about from what router is obtained. // To gather providers from a set of Routers first, you can use the ExecuteAfter timer to delay some Router execution. diff --git a/compsequential.go b/compsequential.go index 5c3255e..5f13e77 100644 --- a/compsequential.go +++ b/compsequential.go @@ -9,6 +9,7 @@ import ( "github.com/ipfs/go-cid" "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/routing" + "github.com/multiformats/go-multihash" ) var _ routing.Routing = &composableSequential{} @@ -27,9 +28,40 @@ func NewComposableSequential(routers []*SequentialRouter) *composableSequential // If some router fails and the IgnoreError flag is true, we continue to the next router. // Context timeout error will be also ignored if the flag is set. func (r *composableSequential) Provide(ctx context.Context, cid cid.Cid, provide bool) error { - return executeSequential(ctx, r.routers, func(ctx context.Context, r routing.Routing) error { - return r.Provide(ctx, cid, provide) - }) + return executeSequential(ctx, r.routers, + func(ctx context.Context, r routing.Routing) error { + return r.Provide(ctx, cid, provide) + }) +} + +// ProvideMany will call all supported Routers sequentially. +func (r *composableSequential) ProvideMany(ctx context.Context, keys []multihash.Multihash) error { + return executeSequential(ctx, r.routers, + func(ctx context.Context, r routing.Routing) error { + pm, ok := r.(ProvideManyRouter) + if !ok { + return nil + } + return pm.ProvideMany(ctx, keys) + }, + ) +} + +// Ready will call all supported ProvideMany Routers sequentially. +// If some of them are not ready, this method will return false. +func (r *composableSequential) Ready() bool { + for _, ro := range r.routers { + pm, ok := ro.Router.(ProvideManyRouter) + if !ok { + continue + } + + if !pm.Ready() { + return false + } + } + + return true } // FindProvidersAsync calls FindProvidersAsync per each router sequentially.