Skip to content

Commit

Permalink
fix: iterate over keys manually in ProvideMany
Browse files Browse the repository at this point in the history
Consumers use type assertions to know if it is safe to send us ProvideMany calls, however we would silently drop them on the floor which is uncool.
Fallback to iterating instead.

Something smarter like doing X many concurrent workers might be nice but I feel this needs to go with a bigger DHT refactor and have ProvideMany operations on all router types.
  • Loading branch information
Jorropo committed Jun 8, 2023
1 parent 4755d1c commit 231f797
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
3 changes: 3 additions & 0 deletions compconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ type SequentialRouter struct {

type ProvideManyRouter interface {
ProvideMany(ctx context.Context, keys []multihash.Multihash) error
}

type ReadyAbleRouter interface {
Ready() bool
}

Expand Down
15 changes: 10 additions & 5 deletions compparallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,16 @@ func (r *composableParallel) Provide(ctx context.Context, cid cid.Cid, provide b
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
if pm, ok := r.(ProvideManyRouter); ok {
return pm.ProvideMany(ctx, keys)
}
return pm.ProvideMany(ctx, keys)

for _, k := range keys {
if err := r.Provide(ctx, cid.NewCidV1(cid.Raw, k), true); err != nil {
return err
}
}
return nil
},
)
}
Expand All @@ -70,7 +75,7 @@ func (r *composableParallel) ProvideMany(ctx context.Context, keys []multihash.M
// 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)
pm, ok := ro.Router.(ReadyAbleRouter)
if !ok {
continue
}
Expand Down
15 changes: 10 additions & 5 deletions compsequential.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,16 @@ func (r *composableSequential) Provide(ctx context.Context, cid cid.Cid, provide
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
if pm, ok := r.(ProvideManyRouter); ok {
return pm.ProvideMany(ctx, keys)
}
return pm.ProvideMany(ctx, keys)

for _, k := range keys {
if err := r.Provide(ctx, cid.NewCidV1(cid.Raw, k), true); err != nil {
return err
}
}
return nil
},
)
}
Expand All @@ -61,7 +66,7 @@ func (r *composableSequential) ProvideMany(ctx context.Context, keys []multihash
// 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)
pm, ok := ro.Router.(ReadyAbleRouter)
if !ok {
continue
}
Expand Down

0 comments on commit 231f797

Please sign in to comment.