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

Commit 2dca30d

Browse files
Merge pull request #279 from libp2p/option
use functional options to configure the swarm
2 parents 084250b + 8134c51 commit 2dca30d

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

swarm.go

+19-12
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ var ErrAddrFiltered = errors.New("address filtered")
4141
// ErrDialTimeout is returned when one a dial times out due to the global timeout
4242
var ErrDialTimeout = errors.New("dial timed out")
4343

44+
type Option func(*Swarm)
45+
46+
// WithConnectionGater sets a connection gater
47+
func WithConnectionGater(gater connmgr.ConnectionGater) Option {
48+
return func(s *Swarm) {
49+
s.gater = gater
50+
}
51+
}
52+
53+
// WithMetrics sets a metrics reporter
54+
func WithMetrics(reporter metrics.Reporter) Option {
55+
return func(s *Swarm) {
56+
s.bwc = reporter
57+
}
58+
}
59+
4460
// Swarm is a connection muxer, allowing connections to other peers to
4561
// be opened and closed, while still using the same Chan for all
4662
// communication. The Chan sends/receives Messages, which note the
@@ -98,17 +114,11 @@ type Swarm struct {
98114
}
99115

100116
// NewSwarm constructs a Swarm.
101-
//
102-
// NOTE: go-libp2p will be moving to dependency injection soon. The variadic
103-
// `extra` interface{} parameter facilitates the future migration. Supported
104-
// elements are:
105-
// - connmgr.ConnectionGater
106-
func NewSwarm(local peer.ID, peers peerstore.Peerstore, bwc metrics.Reporter, extra ...interface{}) *Swarm {
117+
func NewSwarm(local peer.ID, peers peerstore.Peerstore, opts ...Option) *Swarm {
107118
ctx, cancel := context.WithCancel(context.Background())
108119
s := &Swarm{
109120
local: local,
110121
peers: peers,
111-
bwc: bwc,
112122
ctx: ctx,
113123
ctxCancel: cancel,
114124
}
@@ -118,11 +128,8 @@ func NewSwarm(local peer.ID, peers peerstore.Peerstore, bwc metrics.Reporter, ex
118128
s.transports.m = make(map[int]transport.Transport)
119129
s.notifs.m = make(map[network.Notifiee]struct{})
120130

121-
for _, i := range extra {
122-
switch v := i.(type) {
123-
case connmgr.ConnectionGater:
124-
s.gater = v
125-
}
131+
for _, opt := range opts {
132+
opt(s)
126133
}
127134

128135
s.dsync = newDialSync(s.dialWorkerLoop)

swarm_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"github.com/libp2p/go-libp2p-core/peer"
1717
"github.com/libp2p/go-libp2p-core/peerstore"
1818

19-
. "github.com/libp2p/go-libp2p-swarm"
19+
swarm "github.com/libp2p/go-libp2p-swarm"
2020
. "github.com/libp2p/go-libp2p-swarm/testing"
2121

2222
logging "github.com/ipfs/go-log"
@@ -58,14 +58,14 @@ func EchoStreamHandler(stream network.Stream) {
5858
}()
5959
}
6060

61-
func makeDialOnlySwarm(t *testing.T) *Swarm {
61+
func makeDialOnlySwarm(t *testing.T) *swarm.Swarm {
6262
swarm := GenSwarm(t, OptDialOnly)
6363
swarm.SetStreamHandler(EchoStreamHandler)
6464
return swarm
6565
}
6666

67-
func makeSwarms(t *testing.T, num int, opts ...Option) []*Swarm {
68-
swarms := make([]*Swarm, 0, num)
67+
func makeSwarms(t *testing.T, num int, opts ...Option) []*swarm.Swarm {
68+
swarms := make([]*swarm.Swarm, 0, num)
6969
for i := 0; i < num; i++ {
7070
swarm := GenSwarm(t, opts...)
7171
swarm.SetStreamHandler(EchoStreamHandler)
@@ -74,9 +74,9 @@ func makeSwarms(t *testing.T, num int, opts ...Option) []*Swarm {
7474
return swarms
7575
}
7676

77-
func connectSwarms(t *testing.T, ctx context.Context, swarms []*Swarm) {
77+
func connectSwarms(t *testing.T, ctx context.Context, swarms []*swarm.Swarm) {
7878
var wg sync.WaitGroup
79-
connect := func(s *Swarm, dst peer.ID, addr ma.Multiaddr) {
79+
connect := func(s *swarm.Swarm, dst peer.ID, addr ma.Multiaddr) {
8080
// TODO: make a DialAddr func.
8181
s.Peerstore().AddAddr(dst, addr, peerstore.PermanentAddrTTL)
8282
if _, err := s.DialPeer(ctx, dst); err != nil {
@@ -455,7 +455,7 @@ func TestPreventDialListenAddr(t *testing.T) {
455455
remote := peer.ID("foobar")
456456
s.Peerstore().AddAddr(remote, addr, time.Hour)
457457
_, err = s.DialPeer(context.Background(), remote)
458-
if !errors.Is(err, ErrNoGoodAddresses) {
458+
if !errors.Is(err, swarm.ErrNoGoodAddresses) {
459459
t.Fatal("expected dial to fail: %w", err)
460460
}
461461
}

testing/testing.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ func GenSwarm(t *testing.T, opts ...Option) *swarm.Swarm {
113113
ps.AddPrivKey(p.ID, p.PrivKey)
114114
t.Cleanup(func() { ps.Close() })
115115

116-
s := swarm.NewSwarm(p.ID, ps, metrics.NewBandwidthCounter(), cfg.connectionGater)
116+
swarmOpts := []swarm.Option{swarm.WithMetrics(metrics.NewBandwidthCounter())}
117+
if cfg.connectionGater != nil {
118+
swarmOpts = append(swarmOpts, swarm.WithConnectionGater(cfg.connectionGater))
119+
}
120+
s := swarm.NewSwarm(p.ID, ps, swarmOpts...)
117121

118122
upgrader := GenUpgrader(s)
119123
upgrader.ConnGater = cfg.connectionGater

0 commit comments

Comments
 (0)