diff --git a/bitswap/client/internal/session/session.go b/bitswap/client/internal/session/session.go index 942a6c2a5d..3e2a9b53d0 100644 --- a/bitswap/client/internal/session/session.go +++ b/bitswap/client/internal/session/session.go @@ -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") diff --git a/routing/providerquerymanager/providerquerymanager.go b/routing/providerquerymanager/providerquerymanager.go index 2d66a153d3..e0de101636 100644 --- a/routing/providerquerymanager/providerquerymanager.go +++ b/routing/providerquerymanager/providerquerymanager.go @@ -116,7 +116,9 @@ func WithMaxInProcessRequests(count int) Option { } } -// 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 @@ -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 } diff --git a/routing/providerquerymanager/providerquerymanager_test.go b/routing/providerquerymanager/providerquerymanager_test.go index 9d8ac8ed48..b55c1debc9 100644 --- a/routing/providerquerymanager/providerquerymanager_test.go +++ b/routing/providerquerymanager/providerquerymanager_test.go @@ -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++