]> Cypherpunks.ru repositories - gostls13.git/commitdiff
net/http: perform streaming body feature detection once per process
authorDmitri Shuralyov <dmitshur@golang.org>
Sun, 23 Jul 2023 18:19:57 +0000 (14:19 -0400)
committerDmitri Shuralyov <dmitshur@golang.org>
Fri, 28 Jul 2023 02:46:31 +0000 (02:46 +0000)
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 <mdempsky@google.com>
Run-TryBot: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
src/net/http/roundtrip_js.go

index 2826383ce1cd082ab86e144eac43b1c88ce87f0d..dd9efe51c4436ec7f4698a860e3f0660800c5674 100644 (file)
@@ -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) {