friendly apis for constructors

This commit is contained in:
Anton Kaliaev
2018-06-20 12:40:25 +04:00
parent cd11a54f7a
commit 84812145cb
11 changed files with 53 additions and 20 deletions
+14 -2
View File
@@ -78,7 +78,7 @@ type Switch struct {
}
// NewSwitch creates a new Switch with the given config.
func NewSwitch(cfg *config.P2PConfig, metrics *Metrics) *Switch {
func NewSwitch(cfg *config.P2PConfig, options ...func(*Switch)) *Switch {
sw := &Switch{
config: cfg,
reactors: make(map[string]Reactor),
@@ -87,7 +87,7 @@ func NewSwitch(cfg *config.P2PConfig, metrics *Metrics) *Switch {
peers: NewPeerSet(),
dialing: cmn.NewCMap(),
reconnecting: cmn.NewCMap(),
metrics: metrics,
metrics: NopMetrics(),
}
// Ensure we have a completely undeterministic PRNG.
@@ -102,9 +102,21 @@ func NewSwitch(cfg *config.P2PConfig, metrics *Metrics) *Switch {
sw.mConfig = mConfig
sw.BaseService = *cmn.NewBaseService(nil, "P2P Switch", sw)
for _, option := range options {
option(sw)
}
return sw
}
// WithMetrics sets the metrics.
func WithMetrics(metrics *Metrics) func(*Switch) {
return func(sw *Switch) {
sw.metrics = metrics
}
}
//---------------------------------------------------------------------
// Switch setup
+1 -1
View File
@@ -137,7 +137,7 @@ func MakeSwitch(cfg *config.P2PConfig, i int, network, version string, initSwitc
nodeKey := &NodeKey{
PrivKey: crypto.GenPrivKeyEd25519(),
}
sw := NewSwitch(cfg, NopMetrics())
sw := NewSwitch(cfg)
sw.SetLogger(log.TestingLogger())
sw = initSwitch(i, sw)
ni := NodeInfo{