From fd23cf6fa544ff10d8070532555eeaaff4eb9ae9 Mon Sep 17 00:00:00 2001 From: vyzo Date: Wed, 2 Aug 2017 10:58:28 +0300 Subject: [PATCH] basic_host: add the relay after the host construction is complete --- p2p/host/basic/basic_host.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/p2p/host/basic/basic_host.go b/p2p/host/basic/basic_host.go index bc5607281..dde575b55 100644 --- a/p2p/host/basic/basic_host.go +++ b/p2p/host/basic/basic_host.go @@ -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 @@ -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 { @@ -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 }