Skip to content

Commit

Permalink
basic_host: add the relay after the host construction is complete
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Aug 2, 2017
1 parent b97f1c5 commit fd23cf6
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ type HostOpts struct {
ConnManager connmgr.ConnManager

// Relay indicates whether the host should use circuit relay transport
Relay bool
EnableRelay bool

// RelayOpts are options for the relay transport; only meaningful when Relay=true
RelayOpts []circuit.RelayOpt
Expand Down Expand Up @@ -152,13 +152,6 @@ func NewHost(net inet.Network, opts *HostOpts) (*BasicHost, error) {

var relayCtx context.Context
var relayCancel func()
if opts.Relay {
relayCtx, relayCancel = context.WithCancel(context.Background())
err := circuit.AddRelayTransport(relayCtx, h, opts.RelayOpts...)
if err != nil {
return nil, err
}
}

h.proc = goprocess.WithTeardown(func() error {
if h.natmgr != nil {
Expand All @@ -173,6 +166,15 @@ func NewHost(net inet.Network, opts *HostOpts) (*BasicHost, error) {
net.SetConnHandler(h.newConnHandler)
net.SetStreamHandler(h.newStreamHandler)

if opts.EnableRelay {
relayCtx, relayCancel = context.WithCancel(context.Background())
err := circuit.AddRelayTransport(relayCtx, h, opts.RelayOpts...)
if err != nil {
h.Close()
return nil, err
}
}

return h, nil
}

Expand Down

0 comments on commit fd23cf6

Please sign in to comment.