Skip to content

Commit

Permalink
Add WitrFindProviderTimeout option
Browse files Browse the repository at this point in the history
  • Loading branch information
gammazero committed Nov 22, 2024
1 parent f6d672a commit 84f8f13
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
29 changes: 29 additions & 0 deletions bitswap/client/internal/providerquerymanager/options.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package providerquerymanager

import (
"time"
)

const (
defaultFindProviderTimeout = 10 * time.Second
defaultMaxConcurrentFinds = 16
defaultMaxProvidersPerFind = 10
)

type config struct {
findProviderTimeout time.Duration
maxConcurrentFinds int
maxProvidersPerFind int
}
Expand All @@ -14,6 +20,7 @@ type Option func(*config)

func getOpts(opts []Option) config {
cfg := config{
findProviderTimeout: defaultFindProviderTimeout,
maxConcurrentFinds: defaultMaxConcurrentFinds,
maxProvidersPerFind: defaultMaxProvidersPerFind,
}
Expand All @@ -23,19 +30,41 @@ func getOpts(opts []Option) config {
return cfg
}

// WitrFindProviderTimeout configures the maximum amount of time to spend on a
// single find providers attempt. This value can be changed at runtime using
// SetFindProviderTimeout. Use 0 to configure the default.
func WithFindProviderTimeout(to time.Duration) Option {
return func(c *config) {
if to == 0 {
to = defaultFindProviderTimeout
}
c.findProviderTimeout = to
}
}

// WithMaxConcurrentFinds configures the maxmum number of workers that run
// FindProvidersAsync at the same time. Use 0 to configure the default value.
func WithMaxConcurrentFinds(n int) Option {
return func(c *config) {
if n == 0 {
n = defaultMaxConcurrentFinds
} else if n < 0 {
panic("bitswap: WithMaxConcurrentFinds given negative value")
}
c.maxConcurrentFinds = n
}
}

// WithMaxProvidersPerFind configures the maximum number of providers that are
// returned from a single fiond providers attempt. Use 0 to configure the
// default value or use a negative value to find all providers within the
// timeout configured by WithFindProviderTimeout.
func WithMaxProvidersPerFind(n int) Option {
return func(c *config) {
if n == 0 {
n = defaultMaxProvidersPerFind
} else if n < 0 {
n = 0 // 0 means find all
}
c.maxProvidersPerFind = n
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func New(ctx context.Context, network ProviderQueryNetwork, options ...Option) *
providerQueryMessages: make(chan providerQueryMessage),
opts: getOpts(options),
}
pqm.SetFindProviderTimeout(defaultTimeout)
pqm.SetFindProviderTimeout(pqm.opts.findProviderTimeout)
return pqm
}

Expand All @@ -113,9 +113,14 @@ type inProgressRequest struct {
incoming chan peer.ID
}

// SetFindProviderTimeout changes the timeout for finding providers
func (pqm *ProviderQueryManager) SetFindProviderTimeout(findProviderTimeout time.Duration) {
pqm.findProviderTimeout.Store(int64(findProviderTimeout))
// SetFindProviderTimeout changes the timeout for finding providers. Setting a
// value of 0 resets to the value configures when this ProviderQueryManager was
// created.
func (pqm *ProviderQueryManager) SetFindProviderTimeout(timeout time.Duration) {
if timeout == 0 {
timeout = pqm.opts.findProviderTimeout
}
pqm.findProviderTimeout.Store(int64(timeout))
}

// FindProvidersAsync finds providers for the given block.
Expand Down

0 comments on commit 84f8f13

Please sign in to comment.