Skip to content

Commit

Permalink
providerQueryManager: when max == 0 in FindProvidersAsync, use maxPro…
Browse files Browse the repository at this point in the history
…viders option.
  • Loading branch information
hsanjuan committed Nov 21, 2024
1 parent f7da578 commit 19cd67f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
4 changes: 1 addition & 3 deletions bitswap/client/internal/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,7 @@ func (s *Session) findMorePeers(ctx context.Context, c cid.Cid) {
go func(k cid.Cid) {
ctx, span := internal.StartSpan(ctx, "Session.FindMorePeers")
defer span.End()
// Max is set to -1. This means "use the default limit" in the
// provider query manager.
for p := range s.providerFinder.FindProvidersAsync(ctx, k, -1) {
for p := range s.providerFinder.FindProvidersAsync(ctx, k, 0) {
// When a provider indicates that it has a cid, it's equivalent to
// the providing peer sending a HAVE
span.AddEvent("FoundPeer")
Expand Down
11 changes: 6 additions & 5 deletions routing/providerquerymanager/providerquerymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ func WithMaxInProcessRequests(count int) Option {
}

Check warning on line 116 in routing/providerquerymanager/providerquerymanager.go

View check run for this annotation

Codecov / codecov/patch

routing/providerquerymanager/providerquerymanager.go#L112-L116

Added lines #L112 - L116 were not covered by tests
}

// WithMaxProviders is the maximum number of providers that will be looked up per query
// WithMaxProviders is the maximum number of providers that will be looked up
// per query. We only return providers that we can connect to. Defaults to 0,
// which means unbounded.
func WithMaxProviders(count int) Option {
return func(mgr *ProviderQueryManager) error {
mgr.maxProviders = count
Expand Down Expand Up @@ -168,11 +170,10 @@ func (pqm *ProviderQueryManager) setFindProviderTimeout(findProviderTimeout time

// FindProvidersAsync finds providers for the given block. The max parameter
// controls how many will be returned at most. For a provider to be returned,
// we must have successfully connected to it. Setting max to -1 will use the
// configured MaxProviders. Setting max to 0 will return an unbounded number
// of providers.
// we must have successfully connected to it. Setting max to 0 will use the
// configured MaxProviders which defaults to 0 (unbounded).
func (pqm *ProviderQueryManager) FindProvidersAsync(sessionCtx context.Context, k cid.Cid, max int) <-chan peer.AddrInfo {
if max < 0 {
if max == 0 {
max = pqm.maxProviders
}

Expand Down
2 changes: 1 addition & 1 deletion routing/providerquerymanager/providerquerymanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func TestLimitedProviders(t *testing.T) {
providerQueryManager.setFindProviderTimeout(100 * time.Millisecond)
keys := random.Cids(1)

providersChan := providerQueryManager.FindProvidersAsync(ctx, keys[0], -1)
providersChan := providerQueryManager.FindProvidersAsync(ctx, keys[0], 0)
total := 0
for range providersChan {
total++
Expand Down

0 comments on commit 19cd67f

Please sign in to comment.