From 77e42fdeaf98d241dc09f4eb92e09225aea8f4c3 Mon Sep 17 00:00:00 2001 From: Jes Cok Date: Mon, 9 Sep 2024 16:26:41 +0000 Subject: [PATCH] net/http: make use of maps.Clone for Transport.{Clone, RegisterProtocol} Not a big deal, maybe it's a bit clearer that it's cloning a map. Change-Id: I7c85382a01df97d1f58109b2483061e6decdf03a GitHub-Last-Rev: 7a88af7f56ab7ad017081187e226e901752fa8bc GitHub-Pull-Request: golang/go#69357 Reviewed-on: https://go-review.googlesource.com/c/go/+/612015 Auto-Submit: Damien Neil Reviewed-by: Ian Lance Taylor LUCI-TryBot-Result: Go LUCI Reviewed-by: Damien Neil --- src/net/http/transport.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/net/http/transport.go b/src/net/http/transport.go index 26900620f1..ed7c2a52c2 100644 --- a/src/net/http/transport.go +++ b/src/net/http/transport.go @@ -20,6 +20,7 @@ import ( "internal/godebug" "io" "log" + "maps" "net" "net/http/httptrace" "net/http/internal/ascii" @@ -349,9 +350,9 @@ func (t *Transport) Clone() *Transport { *t2.HTTP2 = *t.HTTP2 } if !t.tlsNextProtoWasNil { - npm := map[string]func(authority string, c *tls.Conn) RoundTripper{} - for k, v := range t.TLSNextProto { - npm[k] = v + npm := maps.Clone(t.TLSNextProto) + if npm == nil { + npm = make(map[string]func(authority string, c *tls.Conn) RoundTripper) } t2.TLSNextProto = npm } @@ -830,9 +831,9 @@ func (t *Transport) RegisterProtocol(scheme string, rt RoundTripper) { if _, exists := oldMap[scheme]; exists { panic("protocol " + scheme + " already registered") } - newMap := make(map[string]RoundTripper) - for k, v := range oldMap { - newMap[k] = v + newMap := maps.Clone(oldMap) + if newMap == nil { + newMap = make(map[string]RoundTripper) } newMap[scheme] = rt t.altProto.Store(newMap)