Skip to content

Commit

Permalink
feat(routing/providerquerymanager): allow for configurable WithMaxPro…
Browse files Browse the repository at this point in the history
…viders option
  • Loading branch information
aschmahmann committed Jul 26, 2024
1 parent 5235165 commit e95eeb2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bitswap/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func New(parent context.Context, network bsnet.BitSwapNetwork, bstore blockstore
var defaultQueryManager *rpqm.ProviderQueryManager
if bs.useDefaultLookupManagement {
var err error
defaultQueryManager, err = rpqm.New(ctx, network)
defaultQueryManager, err = rpqm.New(ctx, network, rpqm.WithMaxProviders(10))
if err != nil {
// Should not be possible to hit this
panic(err)

Check warning on line 176 in bitswap/client/client.go

View check run for this annotation

Codecov / codecov/patch

bitswap/client/client.go#L175-L176

Added lines #L175 - L176 were not covered by tests
Expand Down
14 changes: 12 additions & 2 deletions routing/providerquerymanager/providerquerymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
var log = logging.Logger("routing/provqrymgr")

const (
maxProviders = 10
defaultMaxInProcessRequests = 6
defaultMaxProviders = 0
defaultTimeout = 10 * time.Second
)

Expand Down Expand Up @@ -85,6 +85,7 @@ type ProviderQueryManager struct {
findProviderTimeout time.Duration
timeoutMutex sync.RWMutex

maxProviders int
maxInProcessRequests int

// do not touch outside the run loop
Expand All @@ -108,6 +109,14 @@ func WithMaxInProcessRequests(count int) Option {
}

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

View check run for this annotation

Codecov / codecov/patch

routing/providerquerymanager/providerquerymanager.go#L105-L109

Added lines #L105 - L109 were not covered by tests
}

// WithMaxProviders is the maximum number of providers that will be looked up per query
func WithMaxProviders(count int) Option {
return func(mgr *ProviderQueryManager) error {
mgr.maxProviders = count
return nil
}
}

// New initializes a new ProviderQueryManager for a given context and a given
// network provider.
func New(ctx context.Context, network ProviderQueryNetwork, opts ...Option) (*ProviderQueryManager, error) {
Expand All @@ -120,6 +129,7 @@ func New(ctx context.Context, network ProviderQueryNetwork, opts ...Option) (*Pr
inProgressRequestStatuses: make(map[cid.Cid]*inProgressRequestStatus),
findProviderTimeout: defaultTimeout,
maxInProcessRequests: defaultMaxInProcessRequests,
maxProviders: defaultMaxProviders,
}

for _, o := range opts {
Expand Down Expand Up @@ -275,7 +285,7 @@ func (pqm *ProviderQueryManager) findProviderWorker() {
pqm.timeoutMutex.RUnlock()
span := trace.SpanFromContext(findProviderCtx)
span.AddEvent("StartFindProvidersAsync")
providers := pqm.network.FindProvidersAsync(findProviderCtx, k, maxProviders)
providers := pqm.network.FindProvidersAsync(findProviderCtx, k, pqm.maxProviders)
wg := &sync.WaitGroup{}
for p := range providers {
wg.Add(1)
Expand Down

0 comments on commit e95eeb2

Please sign in to comment.