Skip to content

Commit

Permalink
Introduce options to parameterize config of the accelerated DHT client
Browse files Browse the repository at this point in the history
Introduce new options such that the following can be configured, with
fallback onto the existing hardcoded values:
 * network crawl interval
 * success wait fraction
 * bulk send parallelism
 * timeout per operation
  • Loading branch information
masih committed Mar 5, 2023
1 parent 76b0f76 commit 03d91fa
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
15 changes: 10 additions & 5 deletions fullrt/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ type FullRT struct {
//
// Not all of the standard DHT options are supported in this DHT.
func NewFullRT(h host.Host, protocolPrefix protocol.ID, options ...Option) (*FullRT, error) {
var fullrtcfg config
fullrtcfg := config{
crawlInterval: time.Hour,
bulkSendParallelism: 20,
waitFrac: 0.3,
timeoutPerOp: 5 * time.Second,
}
if err := fullrtcfg.apply(options...); err != nil {
return nil, err
}
Expand Down Expand Up @@ -179,12 +184,12 @@ func NewFullRT(h host.Host, protocolPrefix protocol.ID, options ...Option) (*Ful

triggerRefresh: make(chan struct{}),

waitFrac: 0.3,
timeoutPerOp: 5 * time.Second,
waitFrac: fullrtcfg.waitFrac,
timeoutPerOp: fullrtcfg.timeoutPerOp,

crawlerInterval: time.Minute * 60,
crawlerInterval: fullrtcfg.crawlInterval,

bulkSendParallelism: 20,
bulkSendParallelism: fullrtcfg.bulkSendParallelism,
}

rt.wg.Add(1)
Expand Down
48 changes: 48 additions & 0 deletions fullrt/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package fullrt

import (
"fmt"
"time"

kaddht "github.com/libp2p/go-libp2p-kad-dht"
)

type config struct {
dhtOpts []kaddht.Option

crawlInterval time.Duration
waitFrac float64
bulkSendParallelism int
timeoutPerOp time.Duration
}

func (cfg *config) apply(opts ...Option) error {
Expand All @@ -27,3 +33,45 @@ func DHTOption(opts ...kaddht.Option) Option {
return nil
}
}

// WithCrawlInterval sets the interval at which the DHT is crawled to refresh peer store.
// Defaults to 1 hour if unspecified.
func WithCrawlInterval(i time.Duration) Option {
return func(opt *config) error {
opt.crawlInterval = i
return nil
}
}

// WithSuccessWaitFraction sets the fraction of peers to wait for before considering an operation a success defined as a number between (0, 1].
// Defaults to 30% if unspecified.
func WithSuccessWaitFraction(f float64) Option {
return func(opt *config) error {
if f <= 0 || f > 1 {
return fmt.Errorf("success wait fraction must be larger than 0 and smaller or equal to 1; got: %f", f)
}
opt.waitFrac = f
return nil
}
}

// WithBulkSendParallelism sets the maximum degree of parallelism at which messages are sent to other peers. It must be at least 1.
// Defaults to 20 if unspecified.
func WithBulkSendParallelism(b int) Option {
return func(opt *config) error {
if b < 1 {
return fmt.Errorf("bulk send parallelism must be at least 1; got: %d", b)
}
opt.bulkSendParallelism = b
return nil
}
}

// WithTimeoutPerOperation sets the timeout per operation, where operations include putting providers and querying the DHT.
// Defaults to 5 seconds if unspecified.
func WithTimeoutPerOperation(t time.Duration) Option {
return func(opt *config) error {
opt.timeoutPerOp = t
return nil
}
}

0 comments on commit 03d91fa

Please sign in to comment.