Skip to content

Commit 8fd91c4

Browse files
committed
Add ProvideMany
Signed-off-by: Antonio Navarro Perez <[email protected]>
1 parent d27cb11 commit 8fd91c4

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

compconfig.go

+7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package routinghelpers
22

33
import (
4+
"context"
45
"time"
56

67
"github.com/libp2p/go-libp2p-core/routing"
8+
"github.com/multiformats/go-multihash"
79
)
810

911
type ParallelRouter struct {
@@ -18,3 +20,8 @@ type SequentialRouter struct {
1820
IgnoreError bool
1921
Router routing.Routing
2022
}
23+
24+
type ProvideManyRouter interface {
25+
ProvideMany(ctx context.Context, keys []multihash.Multihash) error
26+
Ready() bool
27+
}

compparallel.go

+32
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import (
1111
"github.com/ipfs/go-cid"
1212
"github.com/libp2p/go-libp2p-core/peer"
1313
"github.com/libp2p/go-libp2p-core/routing"
14+
"github.com/multiformats/go-multihash"
1415
)
1516

1617
var _ routing.Routing = &composableParallel{}
18+
var _ ProvideManyRouter = &composableParallel{}
1719

1820
type composableParallel struct {
1921
routers []*ParallelRouter
@@ -38,6 +40,36 @@ func (r *composableParallel) Provide(ctx context.Context, cid cid.Cid, provide b
3840
)
3941
}
4042

43+
// ProvideMany will call all supported Routers in parallel.
44+
func (r *composableParallel) ProvideMany(ctx context.Context, keys []multihash.Multihash) error {
45+
return executeParallel(ctx, r.routers,
46+
func(ctx context.Context, r routing.Routing) error {
47+
pm, ok := r.(ProvideManyRouter)
48+
if !ok {
49+
return nil
50+
}
51+
return pm.ProvideMany(ctx, keys)
52+
},
53+
)
54+
}
55+
56+
// Ready will call all supported ProvideMany Routers SEQUENTIALLY.
57+
// If some of them are not ready, this method will return false.
58+
func (r *composableParallel) Ready() bool {
59+
for _, ro := range r.routers {
60+
pm, ok := ro.Router.(ProvideManyRouter)
61+
if !ok {
62+
continue
63+
}
64+
65+
if !pm.Ready() {
66+
return false
67+
}
68+
}
69+
70+
return true
71+
}
72+
4173
// FindProvidersAsync will execute all Routers in parallel, iterating results from them in unspecified order.
4274
// If count is set, only that amount of elements will be returned without any specification about from what router is obtained.
4375
// To gather providers from a set of Routers first, you can use the ExecuteAfter timer to delay some Router execution.

compsequential.go

+35-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/ipfs/go-cid"
1010
"github.com/libp2p/go-libp2p-core/peer"
1111
"github.com/libp2p/go-libp2p-core/routing"
12+
"github.com/multiformats/go-multihash"
1213
)
1314

1415
var _ routing.Routing = &composableSequential{}
@@ -27,9 +28,40 @@ func NewComposableSequential(routers []*SequentialRouter) *composableSequential
2728
// If some router fails and the IgnoreError flag is true, we continue to the next router.
2829
// Context timeout error will be also ignored if the flag is set.
2930
func (r *composableSequential) Provide(ctx context.Context, cid cid.Cid, provide bool) error {
30-
return executeSequential(ctx, r.routers, func(ctx context.Context, r routing.Routing) error {
31-
return r.Provide(ctx, cid, provide)
32-
})
31+
return executeSequential(ctx, r.routers,
32+
func(ctx context.Context, r routing.Routing) error {
33+
return r.Provide(ctx, cid, provide)
34+
})
35+
}
36+
37+
// ProvideMany will call all supported Routers sequentially.
38+
func (r *composableSequential) ProvideMany(ctx context.Context, keys []multihash.Multihash) error {
39+
return executeSequential(ctx, r.routers,
40+
func(ctx context.Context, r routing.Routing) error {
41+
pm, ok := r.(ProvideManyRouter)
42+
if !ok {
43+
return nil
44+
}
45+
return pm.ProvideMany(ctx, keys)
46+
},
47+
)
48+
}
49+
50+
// Ready will call all supported ProvideMany Routers sequentially.
51+
// If some of them are not ready, this method will return false.
52+
func (r *composableSequential) Ready() bool {
53+
for _, ro := range r.routers {
54+
pm, ok := ro.Router.(ProvideManyRouter)
55+
if !ok {
56+
continue
57+
}
58+
59+
if !pm.Ready() {
60+
return false
61+
}
62+
}
63+
64+
return true
3365
}
3466

3567
// FindProvidersAsync calls FindProvidersAsync per each router sequentially.

0 commit comments

Comments
 (0)