-
Notifications
You must be signed in to change notification settings - Fork 994
/
Copy pathoptions.go
69 lines (58 loc) · 1.71 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
package discovery
import (
"fmt"
"time"
"github.com/ipfs/go-datastore"
)
// 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
datastore datastore.Batching
}
// Option is a function that configures Discovery Parameters
type Option func(*Parameters)
// DefaultParameters returns the default Parameters' configuration values
// for the Discovery module
func DefaultParameters() Parameters {
return Parameters{
PeersLimit: 5,
// based on https://github.com/libp2p/go-libp2p-kad-dht/pull/793
AdvertiseInterval: time.Hour * 22,
}
}
// Validate validates the values in Parameters
func (p *Parameters) Validate() error {
if p.AdvertiseInterval <= 0 {
return fmt.Errorf(
"discovery: invalid option: value AdvertiseInterval %s, %s",
"is 0 or negative.",
"value must be positive",
)
}
return nil
}
// WithPeersLimit is a functional option that Discovery
// uses to set the PeersLimit configuration param
func WithPeersLimit(peersLimit uint) Option {
return func(p *Parameters) {
p.PeersLimit = peersLimit
}
}
// WithAdvertiseInterval is a functional option that Discovery
// uses to set the AdvertiseInterval configuration param
func WithAdvertiseInterval(advInterval time.Duration) Option {
return func(p *Parameters) {
p.AdvertiseInterval = advInterval
}
}
func WithPersistence(ds datastore.Batching) Option {
return func(p *Parameters) {
p.datastore = ds
}
}