diff --git a/bitswap/client/internal/providerquerymanager/options.go b/bitswap/client/internal/providerquerymanager/options.go index be158408b..e48ff2466 100644 --- a/bitswap/client/internal/providerquerymanager/options.go +++ b/bitswap/client/internal/providerquerymanager/options.go @@ -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 } @@ -14,6 +20,7 @@ type Option func(*config) func getOpts(opts []Option) config { cfg := config{ + findProviderTimeout: defaultFindProviderTimeout, maxConcurrentFinds: defaultMaxConcurrentFinds, maxProvidersPerFind: defaultMaxProvidersPerFind, } @@ -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 } diff --git a/bitswap/client/internal/providerquerymanager/providerquerymanager.go b/bitswap/client/internal/providerquerymanager/providerquerymanager.go index 3acd6a963..28feda2ce 100644 --- a/bitswap/client/internal/providerquerymanager/providerquerymanager.go +++ b/bitswap/client/internal/providerquerymanager/providerquerymanager.go @@ -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 } @@ -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.