-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
98 lines (85 loc) · 2.59 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package fullrt
import (
"fmt"
"time"
kaddht "github.com/bpfs/dep2p/dht"
"github.com/bpfs/dep2p/dht/crawler"
"github.com/bpfs/dep2p/dht/providers"
)
type config struct {
dhtOpts []kaddht.Option
crawlInterval time.Duration
waitFrac float64
bulkSendParallelism int
timeoutPerOp time.Duration
crawler crawler.Crawler
pmOpts []providers.Option
}
func (cfg *config) apply(opts ...Option) error {
for i, o := range opts {
if err := o(cfg); err != nil {
return fmt.Errorf("fullrt dht option %d failed: %w", i, err)
}
}
return nil
}
type Option func(opt *config) error
func DHTOption(opts ...kaddht.Option) Option {
return func(c *config) error {
c.dhtOpts = append(c.dhtOpts, opts...)
return nil
}
}
// WithCrawler sets the crawler.Crawler to use in order to crawl the DHT network.
// Defaults to crawler.DefaultCrawler with parallelism of 200.
func WithCrawler(c crawler.Crawler) Option {
return func(opt *config) error {
opt.crawler = c
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
}
}
// WithProviderManagerOptions sets the options to use when instantiating providers.ProviderManager.
func WithProviderManagerOptions(pmOpts ...providers.Option) Option {
return func(opt *config) error {
opt.pmOpts = pmOpts
return nil
}
}