Skip to content

Commit

Permalink
Expand comment on DefaultClientRoundTripper
Browse files Browse the repository at this point in the history
And don't use the http.DefaultRoundTripper and cast
  • Loading branch information
MarcoPolo committed Aug 23, 2023
1 parent 7e37919 commit 5d85daa
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions p2p/http/libp2phttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5d85daa

Please sign in to comment.