@@ -115,12 +115,16 @@ type HostOpts struct {
115
115
116
116
// NewHost constructs a new *BasicHost and activates it by attaching its stream and connection handlers to the given inet.Network.
117
117
func NewHost (ctx context.Context , net inet.Network , opts * HostOpts ) (* BasicHost , error ) {
118
+ bgctx , cancel := context .WithCancel (ctx )
119
+
118
120
h := & BasicHost {
119
121
network : net ,
120
122
mux : msmux .NewMultistreamMuxer (),
121
123
negtimeout : DefaultNegotiationTimeout ,
122
124
AddrsFactory : DefaultAddrsFactory ,
123
125
maResolver : madns .DefaultResolver ,
126
+ ctx : bgctx ,
127
+ cancel : cancel ,
124
128
}
125
129
126
130
h .proc = goprocessctx .WithContextAndTeardown (ctx , func () error {
@@ -138,7 +142,7 @@ func NewHost(ctx context.Context, net inet.Network, opts *HostOpts) (*BasicHost,
138
142
h .ids = opts .IdentifyService
139
143
} else {
140
144
// we can't set this as a default above because it depends on the *BasicHost.
141
- h .ids = identify .NewIDService (ctx , h )
145
+ h .ids = identify .NewIDService (bgctx , h )
142
146
}
143
147
144
148
if uint64 (opts .NegotiationTimeout ) != 0 {
@@ -171,10 +175,6 @@ func NewHost(ctx context.Context, net inet.Network, opts *HostOpts) (*BasicHost,
171
175
net .SetConnHandler (h .newConnHandler )
172
176
net .SetStreamHandler (h .newStreamHandler )
173
177
174
- bgctx , cancel := context .WithCancel (ctx )
175
- h .ctx = bgctx
176
- h .cancel = cancel
177
-
178
178
return h , nil
179
179
}
180
180
0 commit comments