Skip to content

Commit

Permalink
Merge pull request #7883 from ipfs/chore/pin-ls-all-faster
Browse files Browse the repository at this point in the history
Do not fetch recursive pins from pinner unnecessarily
  • Loading branch information
Stebalien authored Mar 29, 2021
2 parents 3f9c3f4 + fb55f09 commit 99309df
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions core/coreapi/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,11 @@ func (p *pinInfo) Err() error {
}

// pinLsAll is an internal function for returning a list of pins
//
// The caller must keep reading results until the channel is closed to prevent
// leaking the goroutine that is fetching pins.
func (api *PinAPI) pinLsAll(ctx context.Context, typeStr string) <-chan coreiface.Pin {
out := make(chan coreiface.Pin)
out := make(chan coreiface.Pin, 1)

keys := cid.NewSet()

Expand Down Expand Up @@ -249,37 +252,34 @@ func (api *PinAPI) pinLsAll(ctx context.Context, typeStr string) <-chan coreifac
go func() {
defer close(out)

var dkeys, rkeys []cid.Cid
var err error
if typeStr == "recursive" || typeStr == "all" {
rkeys, err := api.pinning.RecursiveKeys(ctx)
rkeys, err = api.pinning.RecursiveKeys(ctx)
if err != nil {
out <- &pinInfo{err: err}
return
}
if err := AddToResultKeys(rkeys, "recursive"); err != nil {
if err = AddToResultKeys(rkeys, "recursive"); err != nil {
out <- &pinInfo{err: err}
return
}
}
if typeStr == "direct" || typeStr == "all" {
dkeys, err := api.pinning.DirectKeys(ctx)
dkeys, err = api.pinning.DirectKeys(ctx)
if err != nil {
out <- &pinInfo{err: err}
return
}
if err := AddToResultKeys(dkeys, "direct"); err != nil {
if err = AddToResultKeys(dkeys, "direct"); err != nil {
out <- &pinInfo{err: err}
return
}
}
if typeStr == "all" {
set := cid.NewSet()
rkeys, err := api.pinning.RecursiveKeys(ctx)
if err != nil {
out <- &pinInfo{err: err}
return
}
for _, k := range rkeys {
err := merkledag.Walk(
err = merkledag.Walk(
ctx, merkledag.GetLinksWithDAG(api.dag), k,
set.Visit,
merkledag.SkipRoot(), merkledag.Concurrent(),
Expand All @@ -289,7 +289,7 @@ func (api *PinAPI) pinLsAll(ctx context.Context, typeStr string) <-chan coreifac
return
}
}
if err := AddToResultKeys(set.Keys(), "indirect"); err != nil {
if err = AddToResultKeys(set.Keys(), "indirect"); err != nil {
out <- &pinInfo{err: err}
return
}
Expand All @@ -298,14 +298,14 @@ func (api *PinAPI) pinLsAll(ctx context.Context, typeStr string) <-chan coreifac
// We need to first visit the direct pins that have priority
// without emitting them

dkeys, err := api.pinning.DirectKeys(ctx)
dkeys, err = api.pinning.DirectKeys(ctx)
if err != nil {
out <- &pinInfo{err: err}
return
}
VisitKeys(dkeys)

rkeys, err := api.pinning.RecursiveKeys(ctx)
rkeys, err = api.pinning.RecursiveKeys(ctx)
if err != nil {
out <- &pinInfo{err: err}
return
Expand All @@ -314,7 +314,7 @@ func (api *PinAPI) pinLsAll(ctx context.Context, typeStr string) <-chan coreifac

set := cid.NewSet()
for _, k := range rkeys {
err := merkledag.Walk(
err = merkledag.Walk(
ctx, merkledag.GetLinksWithDAG(api.dag), k,
set.Visit,
merkledag.SkipRoot(), merkledag.Concurrent(),
Expand All @@ -324,7 +324,7 @@ func (api *PinAPI) pinLsAll(ctx context.Context, typeStr string) <-chan coreifac
return
}
}
if err := AddToResultKeys(set.Keys(), "indirect"); err != nil {
if err = AddToResultKeys(set.Keys(), "indirect"); err != nil {
out <- &pinInfo{err: err}
return
}
Expand Down

0 comments on commit 99309df

Please sign in to comment.