Skip to content

Commit ed42d2f

Browse files
committed
Use pointer for Host.ServeMux
1 parent 1af5d3b commit ed42d2f

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

p2p/http/libp2phttp.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,12 @@ type Host struct {
113113
// InsecureAllowHTTP indicates if the server is allowed to serve unencrypted
114114
// HTTP requests over TCP.
115115
InsecureAllowHTTP bool
116-
// ServeMux is the http.ServeMux used by the server to serve requests. The
117-
// zero value is a new serve mux. Users may manually add handlers to this
116+
// ServeMux is the http.ServeMux used by the server to serve requests. If nil,
117+
// new serve mux will be allocated. Users may manually add handlers to this
118118
// mux instead of using `SetHTTPHandler`, but if they do, they should also
119119
// update the WellKnownHandler's protocol mapping.
120-
ServeMux http.ServeMux
120+
ServeMux *http.ServeMux
121+
initializeServeMux sync.Once
121122

122123
// DefaultClientRoundTripper is the default http.RoundTripper for clients to
123124
// use when making requests over an HTTP transport. This must be an
@@ -166,6 +167,12 @@ func (h *Host) httpTransportInit() {
166167
})
167168
}
168169

170+
func (h *Host) serveMuxInit() {
171+
h.initializeServeMux.Do(func() {
172+
h.ServeMux = http.NewServeMux()
173+
})
174+
}
175+
169176
func (h *Host) Addrs() []ma.Multiaddr {
170177
h.httpTransportInit()
171178
<-h.httpTransport.waitingForListeners
@@ -193,6 +200,7 @@ func (h *Host) Serve() error {
193200
}
194201
}
195202

203+
h.serveMuxInit()
196204
h.ServeMux.Handle("/.well-known/libp2p", &h.WellKnownHandler)
197205

198206
h.httpTransportInit()
@@ -227,7 +235,7 @@ func (h *Host) Serve() error {
227235
h.httpTransport.listenAddrs = append(h.httpTransport.listenAddrs, h.StreamHost.Addrs()...)
228236

229237
go func() {
230-
errCh <- http.Serve(listener, &h.ServeMux)
238+
errCh <- http.Serve(listener, h.ServeMux)
231239
}()
232240
}
233241

@@ -274,15 +282,15 @@ func (h *Host) Serve() error {
274282
if parsedAddr.useHTTPS {
275283
go func() {
276284
srv := http.Server{
277-
Handler: &h.ServeMux,
285+
Handler: h.ServeMux,
278286
TLSConfig: h.TLSConfig,
279287
}
280288
errCh <- srv.ServeTLS(l, "", "")
281289
}()
282290
h.httpTransport.listenAddrs = append(h.httpTransport.listenAddrs, listenAddr)
283291
} else if h.InsecureAllowHTTP {
284292
go func() {
285-
errCh <- http.Serve(l, &h.ServeMux)
293+
errCh <- http.Serve(l, h.ServeMux)
286294
}()
287295
h.httpTransport.listenAddrs = append(h.httpTransport.listenAddrs, listenAddr)
288296
} else {
@@ -346,6 +354,7 @@ func (h *Host) SetHTTPHandlerAtPath(p protocol.ID, path string, handler http.Han
346354
path += "/"
347355
}
348356
h.WellKnownHandler.AddProtocolMeta(p, ProtocolMeta{Path: path})
357+
h.serveMuxInit()
349358
h.ServeMux.Handle(path, http.StripPrefix(path, handler))
350359
}
351360

0 commit comments

Comments
 (0)