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.
This commit is contained in:
Steven Allen 2017-11-16 17:34:48 -08:00
parent af895a81d5
commit 62f92f7e73
1 changed files with 10 additions and 15 deletions

View File

@ -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