-
Notifications
You must be signed in to change notification settings - Fork 994
/
Copy pathoptions.go
103 lines (86 loc) · 2.79 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
99
100
101
102
103
package discovery
import (
"fmt"
"time"
"github.com/libp2p/go-libp2p/core/peer"
)
// Parameters is the set of Parameters that must be configured for the Discovery module
type Parameters struct {
// PeersLimit defines the soft limit of FNs to connect to via discovery.
// Set 0 to disable.
PeersLimit uint
// AdvertiseInterval is a interval between advertising sessions.
// NOTE: only full and bridge can advertise themselves.
AdvertiseInterval time.Duration
// AdvertiseRetryTimeout defines time interval between advertise attempts.
AdvertiseRetryTimeout time.Duration
// DiscoveryRetryTimeout defines time interval between discovery attempts
// this is set independently for tests in discover_test.go
DiscoveryRetryTimeout time.Duration
}
// options is the set of options that can be configured for the Discovery module
type options struct {
// onUpdatedPeers will be called on peer set changes
onUpdatedPeers OnUpdatedPeers
// advertise indicates whether the node should also
// advertise to the discovery instance's topic
advertise bool
}
// Option is a function that configures Discovery Parameters
type Option func(*options)
// DefaultParameters returns the default Parameters' configuration values
// for the Discovery module
func DefaultParameters() *Parameters {
return &Parameters{
PeersLimit: 5,
AdvertiseInterval: time.Hour,
AdvertiseRetryTimeout: time.Second,
DiscoveryRetryTimeout: time.Second * 60,
}
}
// TestParameters returns the default Parameters' configuration values
// for the Discovery module, with some changes for configuration
// during tests
func TestParameters() *Parameters {
p := DefaultParameters()
p.AdvertiseInterval = time.Second * 1
p.DiscoveryRetryTimeout = time.Millisecond * 50
return p
}
// Validate validates the values in Parameters
func (p *Parameters) Validate() error {
if p.AdvertiseRetryTimeout <= 0 {
return fmt.Errorf("discovery: advertise retry timeout cannot be zero or negative")
}
if p.DiscoveryRetryTimeout <= 0 {
return fmt.Errorf("discovery: discovery retry timeout cannot be zero or negative")
}
if p.PeersLimit <= 0 {
return fmt.Errorf("discovery: peers limit cannot be zero or negative")
}
if p.AdvertiseInterval <= 0 {
return fmt.Errorf("discovery: advertise interval cannot be zero or negative")
}
return nil
}
// WithOnPeersUpdate chains OnPeersUpdate callbacks on every update of discovered peers list.
func WithOnPeersUpdate(f OnUpdatedPeers) Option {
return func(p *options) {
p.onUpdatedPeers = p.onUpdatedPeers.add(f)
}
}
func WithAdvertise() Option {
return func(p *options) {
p.advertise = true
}
}
func newOptions(opts ...Option) *options {
defaults := &options{
onUpdatedPeers: func(peer.ID, bool) {},
advertise: false,
}
for _, opt := range opts {
opt(defaults)
}
return defaults
}