Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
convert the metrics reporter to an option
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Sep 7, 2021
1 parent a2d7fef commit 23c9e5e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dial_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func TestDialSelf(t *testing.T) {
defer cancel()

self := peer.ID("ABC")
s := NewSwarm(self, nil, nil)
s := NewSwarm(self, nil)
defer s.Close()

// this should fail
Expand Down
10 changes: 8 additions & 2 deletions swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ func WithConnectionGater(gater connmgr.ConnectionGater) Option {
}
}

// WithMetrics sets a metrics reporter
func WithMetrics(reporter metrics.Reporter) Option {
return func(s *Swarm) {
s.bwc = reporter
}
}

// Swarm is a connection muxer, allowing connections to other peers to
// be opened and closed, while still using the same Chan for all
// communication. The Chan sends/receives Messages, which note the
Expand Down Expand Up @@ -107,12 +114,11 @@ type Swarm struct {
}

// NewSwarm constructs a Swarm.
func NewSwarm(local peer.ID, peers peerstore.Peerstore, bwc metrics.Reporter, opts ...Option) *Swarm {
func NewSwarm(local peer.ID, peers peerstore.Peerstore, opts ...Option) *Swarm {
ctx, cancel := context.WithCancel(context.Background())
s := &Swarm{
local: local,
peers: peers,
bwc: bwc,
ctx: ctx,
ctxCancel: cancel,
}
Expand Down
4 changes: 2 additions & 2 deletions testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ func GenSwarm(t *testing.T, opts ...Option) *swarm.Swarm {
ps.AddPrivKey(p.ID, p.PrivKey)
t.Cleanup(func() { ps.Close() })

var swarmOpts []swarm.Option
swarmOpts := []swarm.Option{swarm.WithMetrics(metrics.NewBandwidthCounter())}
if cfg.connectionGater != nil {
swarmOpts = append(swarmOpts, swarm.WithConnectionGater(cfg.connectionGater))
}
s := swarm.NewSwarm(p.ID, ps, metrics.NewBandwidthCounter(), swarmOpts...)
s := swarm.NewSwarm(p.ID, ps, swarmOpts...)

upgrader := GenUpgrader(s)
upgrader.ConnGater = cfg.connectionGater
Expand Down

0 comments on commit 23c9e5e

Please sign in to comment.