Skip to content

Commit

Permalink
refactor pinSet to just return roots first
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Apr 8, 2024
1 parent 4ea2bff commit 558d9c7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ The following emojis are used to highlight certain changes:

* `routing/http/server` now adds `Cache-Control` HTTP header to GET requests: 15 seconds for empty responses, or 5 minutes for responses with providers.
* `routing/http/server` the `/ipns` endpoint is more friendly to users opening URL in web browsers: returns `Content-Disposition` header and defaults to `application/vnd.ipfs.ipns-record` response when `Accept` is missing.
* `provider` now exports a `NewPrioritizedProvider`, which can be used to prioritize certain providers while ignoring duplicates.
* `provider`:
* Exports a `NewPrioritizedProvider`, which can be used to prioritize certain providers while ignoring duplicates.
* 🛠️ `NewPinnedProvider` now prioritizes root blocks, even if `onlyRoots` is set to `false`.

### Changed

Expand Down
64 changes: 36 additions & 28 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,43 +71,55 @@ func NewPinnedProvider(onlyRoots bool, pinning pin.Pinner, fetchConfig fetcher.F
}

func pinSet(ctx context.Context, pinning pin.Pinner, fetchConfig fetcher.Factory, onlyRoots bool) (*cidutil.StreamingSet, error) {
// FIXME: Listing all pins code is duplicated thrice, twice in Kubo and here, maybe more.
// If this were a method of the [pin.Pinner] life would be easier.
set := cidutil.NewStreamingSet()
recursivePins := cidutil.NewSet()

Check warning on line 75 in provider/provider.go

View check run for this annotation

Codecov / codecov/patch

provider/provider.go#L75

Added line #L75 was not covered by tests

go func() {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
defer close(set.New)

for sc := range pinning.DirectKeys(ctx, false) {
// 1. Recursive keys
for sc := range pinning.RecursiveKeys(ctx, false) {

Check warning on line 83 in provider/provider.go

View check run for this annotation

Codecov / codecov/patch

provider/provider.go#L82-L83

Added lines #L82 - L83 were not covered by tests
if sc.Err != nil {
logR.Errorf("reprovide direct pins: %s", sc.Err)
logR.Errorf("reprovide recursive pins: %s", sc.Err)

Check warning on line 85 in provider/provider.go

View check run for this annotation

Codecov / codecov/patch

provider/provider.go#L85

Added line #L85 was not covered by tests
return
}
set.Visitor(ctx)(sc.Pin.Key)
if !onlyRoots {
// Save some bytes.
_ = recursivePins.Visit(sc.Pin.Key)
}
_ = set.Visitor(ctx)(sc.Pin.Key)

Check warning on line 92 in provider/provider.go

View check run for this annotation

Codecov / codecov/patch

provider/provider.go#L88-L92

Added lines #L88 - L92 were not covered by tests
}

session := fetchConfig.NewSession(ctx)
for sc := range pinning.RecursiveKeys(ctx, false) {
// 2. Direct pins
for sc := range pinning.DirectKeys(ctx, false) {

Check warning on line 96 in provider/provider.go

View check run for this annotation

Codecov / codecov/patch

provider/provider.go#L96

Added line #L96 was not covered by tests
if sc.Err != nil {
logR.Errorf("reprovide recursive pins: %s", sc.Err)
logR.Errorf("reprovide direct pins: %s", sc.Err)

Check warning on line 98 in provider/provider.go

View check run for this annotation

Codecov / codecov/patch

provider/provider.go#L98

Added line #L98 was not covered by tests
return
}
set.Visitor(ctx)(sc.Pin.Key)
if !onlyRoots {
err := fetcherhelpers.BlockAll(ctx, session, cidlink.Link{Cid: sc.Pin.Key}, func(res fetcher.FetchResult) error {
clink, ok := res.LastBlockLink.(cidlink.Link)
if ok {
set.Visitor(ctx)(clink.Cid)
}
return nil
})
if err != nil {
logR.Errorf("reprovide indirect pins: %s", err)
return
_ = set.Visitor(ctx)(sc.Pin.Key)

Check warning on line 101 in provider/provider.go

View check run for this annotation

Codecov / codecov/patch

provider/provider.go#L101

Added line #L101 was not covered by tests
}

if onlyRoots {
return
}

Check warning on line 106 in provider/provider.go

View check run for this annotation

Codecov / codecov/patch

provider/provider.go#L104-L106

Added lines #L104 - L106 were not covered by tests

// 3. Go through recursive pins to fetch remaining blocks if we want more
// than just roots.
session := fetchConfig.NewSession(ctx)
err := recursivePins.ForEach(func(c cid.Cid) error {
return fetcherhelpers.BlockAll(ctx, session, cidlink.Link{Cid: c}, func(res fetcher.FetchResult) error {
clink, ok := res.LastBlockLink.(cidlink.Link)
if ok {
_ = set.Visitor(ctx)(clink.Cid)

Check warning on line 115 in provider/provider.go

View check run for this annotation

Codecov / codecov/patch

provider/provider.go#L110-L115

Added lines #L110 - L115 were not covered by tests
}
}
return nil

Check warning on line 117 in provider/provider.go

View check run for this annotation

Codecov / codecov/patch

provider/provider.go#L117

Added line #L117 was not covered by tests
})
})
if err != nil {
logR.Errorf("reprovide direct pins: %s", err)
return

Check warning on line 122 in provider/provider.go

View check run for this annotation

Codecov / codecov/patch

provider/provider.go#L120-L122

Added lines #L120 - L122 were not covered by tests
}
}()

Expand All @@ -120,7 +132,7 @@ func NewPrioritizedProvider(priorityCids KeyChanFunc, otherCids KeyChanFunc) Key

go func() {
defer close(outCh)
visited := map[string]struct{}{}
visited := cidutil.NewSet()

handleStream := func(stream KeyChanFunc, markVisited bool) error {
ch, err := stream(ctx)
Expand All @@ -137,11 +149,7 @@ func NewPrioritizedProvider(priorityCids KeyChanFunc, otherCids KeyChanFunc) Key
return nil
}

// Bytes of the multihash of the CID as key. Major routing systems
// in use are multihash based. Advertising 2 CIDs with the same
// multihash would not be very useful.
str := string(c.Hash())
if _, ok := visited[str]; ok {
if visited.Has(c) {
continue
}

Expand All @@ -150,7 +158,7 @@ func NewPrioritizedProvider(priorityCids KeyChanFunc, otherCids KeyChanFunc) Key
return nil

Check warning on line 158 in provider/provider.go

View check run for this annotation

Codecov / codecov/patch

provider/provider.go#L157-L158

Added lines #L157 - L158 were not covered by tests
case outCh <- c:
if markVisited {
visited[str] = struct{}{}
_ = visited.Visit(c)
}
}
}
Expand Down

0 comments on commit 558d9c7

Please sign in to comment.