Skip to content

Commit

Permalink
gc: restructure GC and make it stateful
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
  • Loading branch information
Kubuxu committed Sep 11, 2017
1 parent b08d07b commit d292196
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 156 deletions.
6 changes: 5 additions & 1 deletion core/commands/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ order to reclaim hard disk space.

streamErrors, _, _ := res.Request().Option("stream-errors").Bool()

gcOutChan := corerepo.GarbageCollectAsync(n, req.Context())
gcOutChan, err := corerepo.GarbageCollectAsync(n, req.Context())
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

outChan := make(chan interface{}, cap(gcOutChan))
res.SetOutput((<-chan interface{})(outChan))
Expand Down
29 changes: 18 additions & 11 deletions core/corerepo/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,10 @@ func BestEffortRoots(filesRoot *mfs.Root) ([]*cid.Cid, error) {
}

func GarbageCollect(n *core.IpfsNode, ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel() // in case error occurs during operation
roots, err := BestEffortRoots(n.FilesRoot)
rmed, err := GarbageCollectAsync(n, ctx)
if err != nil {
return err
}
rmed := gc.GC(ctx, n.Blockstore, n.DAG, n.Pinning, roots)

return CollectResult(ctx, rmed, nil)
}
Expand Down Expand Up @@ -145,16 +142,26 @@ func (e *MultiError) Error() string {
return buf.String()
}

func GarbageCollectAsync(n *core.IpfsNode, ctx context.Context) <-chan gc.Result {
roots, err := BestEffortRoots(n.FilesRoot)
func GarbageCollectAsync(n *core.IpfsNode, ctx context.Context) (<-chan gc.Result, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel() // in case error occurs during operation
g, err := gc.NewGC(n.Blockstore, n.DAG)
if err != nil {
out := make(chan gc.Result)
out <- gc.Result{Error: err}
close(out)
return out
return nil, err
}

for _, p := range n.Pinning.PinSources() {
err = g.AddPinSource(p)
if err != nil {
return nil, err
}
}
err = g.AddPinSource(*n.FilesRoot.PinSource())
if err != nil {
return nil, err
}

return gc.GC(ctx, n.Blockstore, n.DAG, n.Pinning, roots)
return g.Run(ctx), nil
}

func PeriodicGC(ctx context.Context, node *core.IpfsNode) error {
Expand Down
Loading

0 comments on commit d292196

Please sign in to comment.