x/net/http2: 新的封装的 http2 实现 (go1.27+) 缺少单一飞行机制
What did you do? run this code to count dial calls in http2 package main() { totalRequests := 100 targetURL := "https://cp.Cloudflare.com/cdn-cgi/trace" var dialCount atomic.Int64 client := &http.Client{ Transport: &http2.Transport{ DialTLSContext: func(ctx context.Context, network, addr string, cfg *tls.Config) (c net.Conn, err error) { dialCount.Add(1) dialer := &tls.Dialer{ Config: cfg } return dialer.DialContext(ctx, network, addr) }, }, } var wg sync.WaitGroup start := make(chan struct{}) for range totalRequests { wg.Add(1) go func() { defer wg.Done() <-start res, err := client.Get(targetURL) if err == nil { io.Copy(io.Discard, res.Body) res.Body.Close() } }() } close(start) wg.Wait() fmt.Printf("Total Dial Calls: %d\n", dialCount.Load()) } What did you expect to see? In the old http2 implementation, instantly sending multiple requests will only make one TCP dial, this is the expected behavior of http2 run code above with this go run -tags http2legacy . And it will return Total Dial Calls: 1 I understand that the HTTP package needs to dial multiple connections because the server HTTP version is unknown, but we have prior knowledge in net/x/http2, and perhaps we can add a config to enable the single flight mechanism in the http package (or automatically use when alpn only contains h2?)
内容来源: golang/go