Skip to content

Commit b95e413

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

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-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: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,26 @@ 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+
loop:
97+
for {
98+
select {
99+
case res, ok := <-gcOut:
100+
if !ok {
101+
break loop
102+
}
103+
if res.Error != nil {
104+
errors = append(errors, res.Error)
105+
} else if res.KeyRemoved != nil && cb != nil {
106+
cb(res.KeyRemoved)
107+
}
108+
case <-ctx.Done():
109+
errors = append(errors, ctx.Err())
110+
break loop
101111
}
102112
}
103113

0 commit comments

Comments
 (0)