From: Dmitri Shuralyov Date: Sun, 23 Jul 2023 18:19:57 +0000 (-0400) Subject: net/http: perform streaming body feature detection once per process X-Git-Tag: go1.22rc1~1505 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=8488309192b0ed4b393e2f7b2a93491139ff8ad0;p=gostls13.git 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 --- 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) {