From 62f92f7e739bcbe932111c6dcd16efad771fda41 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 16 Nov 2017 17:34:48 -0800 Subject: [PATCH] always make the host context cancelable There's really no reason to *only* do this if we have relays enabled. Doing it this way makes `go vet` happier and reduces conditional logic. --- p2p/host/basic/basic_host.go | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/p2p/host/basic/basic_host.go b/p2p/host/basic/basic_host.go index a2bd21e..d57cd13 100644 --- a/p2p/host/basic/basic_host.go +++ b/p2p/host/basic/basic_host.go @@ -115,6 +115,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(ctx context.Context, net inet.Network, opts *HostOpts) (*BasicHost, error) { + ctx, cancel := context.WithCancel(ctx) h := &BasicHost{ network: net, mux: msmux.NewMultistreamMuxer(), @@ -123,6 +124,14 @@ func NewHost(ctx context.Context, net inet.Network, opts *HostOpts) (*BasicHost, maResolver: madns.DefaultResolver, } + h.proc = goprocess.WithTeardown(func() error { + if h.natmgr != nil { + h.natmgr.Close() + } + cancel() + return h.Network().Close() + }) + if opts.MultistreamMuxer != nil { h.mux = opts.MultistreamMuxer } @@ -162,25 +171,11 @@ func NewHost(ctx context.Context, net inet.Network, opts *HostOpts) (*BasicHost, net.Notify(h.cmgr.Notifee()) } - var relayCtx context.Context - var relayCancel func() - - h.proc = goprocess.WithTeardown(func() error { - if h.natmgr != nil { - h.natmgr.Close() - } - if relayCancel != nil { - relayCancel() - } - return h.Network().Close() - }) - net.SetConnHandler(h.newConnHandler) net.SetStreamHandler(h.newStreamHandler) if opts.EnableRelay { - relayCtx, relayCancel = context.WithCancel(ctx) - err := circuit.AddRelayTransport(relayCtx, h, opts.RelayOpts...) + err := circuit.AddRelayTransport(ctx, h, opts.RelayOpts...) if err != nil { h.Close() return nil, err -- GitLab