github.com/celestiaorg/celestia-node@v0.15.0-beta.1/share/p2p/discovery/options.go (about) 1 package discovery 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/libp2p/go-libp2p/core/peer" 8 ) 9 10 // Parameters is the set of Parameters that must be configured for the Discovery module 11 type Parameters struct { 12 // PeersLimit defines the soft limit of FNs to connect to via discovery. 13 // Set 0 to disable. 14 PeersLimit uint 15 // AdvertiseInterval is a interval between advertising sessions. 16 // Set -1 to disable. 17 // NOTE: only full and bridge can advertise themselves. 18 AdvertiseInterval time.Duration 19 } 20 21 // options is the set of options that can be configured for the Discovery module 22 type options struct { 23 // onUpdatedPeers will be called on peer set changes 24 onUpdatedPeers OnUpdatedPeers 25 } 26 27 // Option is a function that configures Discovery Parameters 28 type Option func(*options) 29 30 // DefaultParameters returns the default Parameters' configuration values 31 // for the Discovery module 32 func DefaultParameters() *Parameters { 33 return &Parameters{ 34 PeersLimit: 5, 35 AdvertiseInterval: time.Hour, 36 } 37 } 38 39 // Validate validates the values in Parameters 40 func (p *Parameters) Validate() error { 41 if p.PeersLimit <= 0 { 42 return fmt.Errorf("discovery: peers limit cannot be zero or negative") 43 } 44 45 if p.AdvertiseInterval <= 0 { 46 return fmt.Errorf("discovery: advertise interval cannot be zero or negative") 47 } 48 return nil 49 } 50 51 // WithOnPeersUpdate chains OnPeersUpdate callbacks on every update of discovered peers list. 52 func WithOnPeersUpdate(f OnUpdatedPeers) Option { 53 return func(p *options) { 54 p.onUpdatedPeers = p.onUpdatedPeers.add(f) 55 } 56 } 57 58 func newOptions(opts ...Option) *options { 59 defaults := &options{ 60 onUpdatedPeers: func(peer.ID, bool) {}, 61 } 62 63 for _, opt := range opts { 64 opt(defaults) 65 } 66 return defaults 67 }