Skip to content

Commit dccecff

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

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-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

+34
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,38 @@ 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 in parallel.
57+
// If some of them are not ready, this method will return false.
58+
func (r *composableParallel) Ready() bool {
59+
ready, _ := getValueOrErrorParallel(context.Background(), r.routers,
60+
func(ctx context.Context, r routing.Routing) (bool, bool, error) {
61+
pm, ok := r.(ProvideManyRouter)
62+
if !ok {
63+
return false, true, nil
64+
}
65+
66+
ready := pm.Ready()
67+
68+
return ready, !ready, nil
69+
},
70+
)
71+
72+
return ready
73+
}
74+
4175
// FindProvidersAsync will execute all Routers in parallel, iterating results from them in unspecified order.
4276
// If count is set, only that amount of elements will be returned without any specification about from what router is obtained.
4377
// To gather providers from a set of Routers first, you can use the ExecuteAfter timer to delay some Router execution.

compsequential.go

+37-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,42 @@ 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+
ready, _ := getValueOrErrorSequential(context.Background(), r.routers,
54+
func(ctx context.Context, r routing.Routing) (bool, bool, error) {
55+
pm, ok := r.(ProvideManyRouter)
56+
if !ok {
57+
return false, true, nil
58+
}
59+
60+
ready := pm.Ready()
61+
62+
return ready, !ready, nil
63+
},
64+
)
65+
66+
return ready
3367
}
3468

3569
// FindProvidersAsync calls FindProvidersAsync per each router sequentially.

0 commit comments

Comments
 (0)