-
Notifications
You must be signed in to change notification settings - Fork 885
/
options.go
67 lines (56 loc) · 1.68 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
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.
// Set -1 to disable.
// NOTE: only full and bridge can advertise themselves.
AdvertiseInterval 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
}
// 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,
}
}
// Validate validates the values in Parameters
func (p *Parameters) Validate() error {
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 newOptions(opts ...Option) *options {
defaults := &options{
onUpdatedPeers: func(peer.ID, bool) {},
}
for _, opt := range opts {
opt(defaults)
}
return defaults
}