Skip to content

Commit

Permalink
gc: pass context into CollectResult
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Feb 24, 2017
1 parent 9cfba7d commit b95e413
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion core/commands/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ order to reclaim hard disk space.
}
}
} else {
lastErr = corerepo.CollectResult(gcOutChan, func(k *cid.Cid) {
lastErr = corerepo.CollectResult(req.Context(), gcOutChan, func(k *cid.Cid) {
outChan <- &GcResult{Key: k}
})
}
Expand Down
24 changes: 17 additions & 7 deletions core/corerepo/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,26 @@ func GarbageCollect(n *core.IpfsNode, ctx context.Context) error {
}
rmed := gc.GC(ctx, n.Blockstore, n.DAG, n.Pinning, roots)

return CollectResult(rmed, nil)
return CollectResult(ctx, rmed, nil)
}

func CollectResult(gcOut <-chan gc.Result, cb func(*cid.Cid)) error {
func CollectResult(ctx context.Context, gcOut <-chan gc.Result, cb func(*cid.Cid)) error {
var errors []error
for res := range gcOut {
if res.Error != nil {
errors = append(errors, res.Error)
} else if res.KeyRemoved != nil && cb != nil {
cb(res.KeyRemoved)
loop:
for {
select {
case res, ok := <-gcOut:
if !ok {
break loop
}
if res.Error != nil {
errors = append(errors, res.Error)
} else if res.KeyRemoved != nil && cb != nil {
cb(res.KeyRemoved)
}
case <-ctx.Done():
errors = append(errors, ctx.Err())
break loop
}
}

Expand Down

0 comments on commit b95e413

Please sign in to comment.