From 5d85daa6f1484ff4d653cab4f2338d65295857c9 Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Wed, 23 Aug 2023 09:58:17 -0700 Subject: [PATCH] Expand comment on DefaultClientRoundTripper And don't use the http.DefaultRoundTripper and cast --- p2p/http/libp2phttp.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/p2p/http/libp2phttp.go b/p2p/http/libp2phttp.go index aff3477b81..6f1bae942d 100644 --- a/p2p/http/libp2phttp.go +++ b/p2p/http/libp2phttp.go @@ -121,7 +121,11 @@ type Host struct { // update the WellKnownHandler's protocol mapping. ServeMux http.ServeMux - // DefaultClientRoundTripper is the default http.RoundTripper for clients + // DefaultClientRoundTripper is the default http.RoundTripper for clients to + // use when making requests over an HTTP transport. This must be an + // `*http.Transport` type so that the transport can be cloned and the + // `TLSClientConfig` field can be configured. If unset, it will create a new + // `http.Transport` on first use. DefaultClientRoundTripper *http.Transport // WellKnownHandler is the http handler for the `.well-known/libp2p` @@ -133,7 +137,10 @@ type Host struct { peerMetadata *lru.Cache[peer.ID, PeerMeta] // createHTTPTransport is used to lazily create the httpTransport in a thread-safe way. createHTTPTransport sync.Once - httpTransport *httpTransport + // createDefaultClientRoundTripper is used to lazily create the default + // client round tripper in a thread-safe way. + createDefaultClientRoundTripper sync.Once + httpTransport *httpTransport } type httpTransport struct { @@ -579,10 +586,12 @@ func (h *Host) NewRoundTripper(server peer.AddrInfo, opts ...RoundTripperOption) scheme = "https" } + h.createDefaultClientRoundTripper.Do(func() { + if h.DefaultClientRoundTripper == nil { + h.DefaultClientRoundTripper = &http.Transport{} + } + }) rt := h.DefaultClientRoundTripper - if rt == nil { - rt = http.DefaultTransport.(*http.Transport) - } ownRoundtripper := false if parsed.sni != parsed.host { // We have a different host and SNI (e.g. using an IP address but specifying a SNI)