Skip to content

Commit

Permalink
basic_host: NewHost: don't panic on relay errors, return an error ins…
Browse files Browse the repository at this point in the history
…tead

The legacy interface stays unchanged with a panic though.
  • Loading branch information
vyzo committed Aug 1, 2017
1 parent 3b6d611 commit 7f1ffcb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
17 changes: 11 additions & 6 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ type HostOpts struct {
}

// NewHost constructs a new *BasicHost and activates it by attaching its stream and connection handlers to the given inet.Network.
func NewHost(net inet.Network, opts *HostOpts) *BasicHost {
func NewHost(net inet.Network, opts *HostOpts) (*BasicHost, error) {
h := &BasicHost{
network: net,
mux: msmux.NewMultistreamMuxer(),
Expand Down Expand Up @@ -156,9 +156,7 @@ func NewHost(net inet.Network, opts *HostOpts) *BasicHost {
relayCtx, relayCancel = context.WithCancel(context.Background())
err := circuit.AddRelayTransport(relayCtx, h, opts.RelayOpts...)
if err != nil {
// perhaps inappropriate, but otherwise we have to change the interface
// to return an error, which will nost likely lead to a fatality anyway
panic(err)
return nil, err
}
}

Expand All @@ -175,7 +173,7 @@ func NewHost(net inet.Network, opts *HostOpts) *BasicHost {
net.SetConnHandler(h.newConnHandler)
net.SetStreamHandler(h.newStreamHandler)

return h
return h, nil
}

// New constructs and sets up a new *BasicHost with given Network and options.
Expand All @@ -200,7 +198,14 @@ func New(net inet.Network, opts ...interface{}) *BasicHost {
}
}

return NewHost(net, hostopts)
h, err := NewHost(net, hostopts)
if err != nil {
// this cannot happen with legacy options
// plus we want to keep the (deprecated) legacy interface unchanged
panic(err)
}

return h
}

// newConnHandler is the remote-opened conn handler for inet.Network
Expand Down
6 changes: 5 additions & 1 deletion p2p/net/mock/mock_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ func (mn *mocknet) AddPeerWithPeerstore(p peer.ID, ps pstore.Peerstore) (host.Ho
opts := &bhost.HostOpts{
NegotiationTimeout: -1,
}
h := bhost.NewHost(n, opts)

h, err := bhost.NewHost(n, opts)
if err != nil {
return nil, err
}

mn.proc.AddChild(n.proc)

Expand Down

0 comments on commit 7f1ffcb

Please sign in to comment.