Skip to content

Commit a40d15c

Browse files
committed
gc: pass context into CollectResult
License: MIT Signed-off-by: Kevin Atkinson <[email protected]>
1 parent 9cfba7d commit a40d15c

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

core/commands/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ order to reclaim hard disk space.
9090
}
9191
}
9292
} else {
93-
lastErr = corerepo.CollectResult(gcOutChan, func(k *cid.Cid) {
93+
lastErr = corerepo.CollectResult(req.Context(), gcOutChan, func(k *cid.Cid) {
9494
outChan <- &GcResult{Key: k}
9595
})
9696
}

core/corerepo/gc.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,25 @@ func GarbageCollect(n *core.IpfsNode, ctx context.Context) error {
8888
}
8989
rmed := gc.GC(ctx, n.Blockstore, n.DAG, n.Pinning, roots)
9090

91-
return CollectResult(rmed, nil)
91+
return CollectResult(ctx, rmed, nil)
9292
}
9393

94-
func CollectResult(gcOut <-chan gc.Result, cb func(*cid.Cid)) error {
94+
func CollectResult(ctx context.Context, gcOut <-chan gc.Result, cb func(*cid.Cid)) error {
9595
var errors []error
96-
for res := range gcOut {
97-
if res.Error != nil {
98-
errors = append(errors, res.Error)
99-
} else if res.KeyRemoved != nil && cb != nil {
100-
cb(res.KeyRemoved)
96+
for {
97+
select {
98+
case res, ok := <-gcOut:
99+
if !ok {
100+
break
101+
}
102+
if res.Error != nil {
103+
errors = append(errors, res.Error)
104+
} else if res.KeyRemoved != nil && cb != nil {
105+
cb(res.KeyRemoved)
106+
}
107+
case <-ctx.Done():
108+
errors = append(errors, ctx.Err())
109+
break
101110
}
102111
}
103112

0 commit comments

Comments
 (0)