From 8488309192b0ed4b393e2f7b2a93491139ff8ad0 Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Sun, 23 Jul 2023 14:19:57 -0400 Subject: [PATCH] net/http: perform streaming body feature detection once per process As far a I can tell, there's currently no situation where this feature detection will report a different result per request, so default to doing once per process until there's evidence that doing it more often is worthwhile. Change-Id: I567d3dbd847af2f49f2e83cd9eb0ae61d82c1f83 Reviewed-on: https://go-review.googlesource.com/c/go/+/513459 Reviewed-by: Matthew Dempsky Run-TryBot: Johan Brandhorst-Satzkorn TryBot-Result: Gopher Robot Reviewed-by: Dmitri Shuralyov Reviewed-by: Johan Brandhorst-Satzkorn --- src/net/http/roundtrip_js.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/net/http/roundtrip_js.go b/src/net/http/roundtrip_js.go index 2826383ce1..dd9efe51c4 100644 --- a/src/net/http/roundtrip_js.go +++ b/src/net/http/roundtrip_js.go @@ -12,6 +12,7 @@ import ( "io" "strconv" "strings" + "sync" "syscall/js" ) @@ -57,7 +58,7 @@ var jsFetchDisabled = js.Global().Get("process").Type() == js.TypeObject && // Determine whether the JS runtime supports streaming request bodies. // Courtesy: https://developer.chrome.com/articles/fetch-streaming-requests/#feature-detection -func supportsPostRequestStreams() bool { +var supportsPostRequestStreams = sync.OnceValue(func() bool { requestOpt := js.Global().Get("Object").New() requestBody := js.Global().Get("ReadableStream").New() @@ -85,7 +86,7 @@ func supportsPostRequestStreams() bool { hasContentTypeHeader := requestObject.Get("headers").Call("has", "Content-Type").Bool() return duplexCalled && !hasContentTypeHeader -} +}) // RoundTrip implements the RoundTripper interface using the WHATWG Fetch API. func (t *Transport) RoundTrip(req *Request) (*Response, error) { -- 2.50.0