add an error return value to the constructor

This commit is contained in:
Marten Seemann 2021-09-08 17:58:30 +01:00
parent 6343fe4abd
commit 96eb4c7e1c
2 changed files with 14 additions and 7 deletions

View File

@ -41,19 +41,21 @@ var ErrAddrFiltered = errors.New("address filtered")
// ErrDialTimeout is returned when one a dial times out due to the global timeout // ErrDialTimeout is returned when one a dial times out due to the global timeout
var ErrDialTimeout = errors.New("dial timed out") var ErrDialTimeout = errors.New("dial timed out")
type Option func(*Swarm) type Option func(*Swarm) error
// WithConnectionGater sets a connection gater // WithConnectionGater sets a connection gater
func WithConnectionGater(gater connmgr.ConnectionGater) Option { func WithConnectionGater(gater connmgr.ConnectionGater) Option {
return func(s *Swarm) { return func(s *Swarm) error {
s.gater = gater s.gater = gater
return nil
} }
} }
// WithMetrics sets a metrics reporter // WithMetrics sets a metrics reporter
func WithMetrics(reporter metrics.Reporter) Option { func WithMetrics(reporter metrics.Reporter) Option {
return func(s *Swarm) { return func(s *Swarm) error {
s.bwc = reporter s.bwc = reporter
return nil
} }
} }
@ -114,7 +116,7 @@ type Swarm struct {
} }
// NewSwarm constructs a Swarm. // NewSwarm constructs a Swarm.
func NewSwarm(local peer.ID, peers peerstore.Peerstore, opts ...Option) *Swarm { func NewSwarm(local peer.ID, peers peerstore.Peerstore, opts ...Option) (*Swarm, error) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
s := &Swarm{ s := &Swarm{
local: local, local: local,
@ -129,13 +131,15 @@ func NewSwarm(local peer.ID, peers peerstore.Peerstore, opts ...Option) *Swarm {
s.notifs.m = make(map[network.Notifiee]struct{}) s.notifs.m = make(map[network.Notifiee]struct{})
for _, opt := range opts { for _, opt := range opts {
opt(s) if err := opt(s); err != nil {
return nil, err
}
} }
s.dsync = newDialSync(s.dialWorkerLoop) s.dsync = newDialSync(s.dialWorkerLoop)
s.limiter = newDialLimiter(s.dialAddr) s.limiter = newDialLimiter(s.dialAddr)
s.backf.init(s.ctx) s.backf.init(s.ctx)
return s return s, nil
} }
func (s *Swarm) Close() error { func (s *Swarm) Close() error {

View File

@ -3,6 +3,8 @@ package testing
import ( import (
"testing" "testing"
"github.com/stretchr/testify/require"
csms "github.com/libp2p/go-conn-security-multistream" csms "github.com/libp2p/go-conn-security-multistream"
"github.com/libp2p/go-libp2p-core/connmgr" "github.com/libp2p/go-libp2p-core/connmgr"
"github.com/libp2p/go-libp2p-core/control" "github.com/libp2p/go-libp2p-core/control"
@ -117,7 +119,8 @@ func GenSwarm(t *testing.T, opts ...Option) *swarm.Swarm {
if cfg.connectionGater != nil { if cfg.connectionGater != nil {
swarmOpts = append(swarmOpts, swarm.WithConnectionGater(cfg.connectionGater)) swarmOpts = append(swarmOpts, swarm.WithConnectionGater(cfg.connectionGater))
} }
s := swarm.NewSwarm(p.ID, ps, swarmOpts...) s, err := swarm.NewSwarm(p.ID, ps, swarmOpts...)
require.NoError(t, err)
upgrader := GenUpgrader(s) upgrader := GenUpgrader(s)
upgrader.ConnGater = cfg.connectionGater upgrader.ConnGater = cfg.connectionGater