diff --git a/common/httpx/httpx.go b/common/httpx/httpx.go index 039f4c4c..594c7003 100644 --- a/common/httpx/httpx.go +++ b/common/httpx/httpx.go @@ -8,7 +8,6 @@ import ( "net" "net/http" "net/url" - "os" "strconv" "strings" "time" @@ -153,9 +152,8 @@ func New(options *Options) (*HTTPX, error) { DisableKeepAlives: true, } - if httpx.Options.Protocol == "http11" { + if isHTTP11Protocol(httpx.Options.Protocol) { // disable http2 - _ = os.Setenv("GODEBUG", "http2client=0") transport.TLSNextProto = map[string]func(string, *tls.Conn) http.RoundTripper{} } @@ -182,6 +180,10 @@ func New(options *Options) (*HTTPX, error) { Timeout: httpx.Options.Timeout, CheckRedirect: redirectFunc, }, retryablehttpOptions) + if isHTTP11Protocol(httpx.Options.Protocol) { + // Keep retryablehttp-go on HTTP/1.1 as well when it retries internally. + httpx.client.HTTPClient2 = httpx.client.HTTPClient + } transport2 := &http2.Transport{ TLSClientConfig: &tls.Config{ @@ -212,6 +214,10 @@ func New(options *Options) (*HTTPX, error) { return httpx, nil } +func isHTTP11Protocol(protocol Proto) bool { + return strings.EqualFold(string(protocol), string(HTTP11)) +} + // Do http request func (h *HTTPX) Do(req *retryablehttp.Request, unsafeOptions UnsafeOptions) (*Response, error) { timeStart := time.Now() diff --git a/common/httpx/httpx_test.go b/common/httpx/httpx_test.go index 7da6ad12..82e5e28a 100644 --- a/common/httpx/httpx_test.go +++ b/common/httpx/httpx_test.go @@ -28,3 +28,29 @@ func TestDo(t *testing.T) { require.Greater(t, len(resp.Raw), 800) }) } + +func TestHTTP11DisablesRetryableHTTP2Fallback(t *testing.T) { + opts := DefaultOptions + opts.Protocol = HTTP11 + + ht, err := New(&opts) + require.NoError(t, err) + require.Same(t, ht.client.HTTPClient, ht.client.HTTPClient2) +} + +func TestHTTP11MixedCaseDisablesRetryableHTTP2Fallback(t *testing.T) { + opts := DefaultOptions + opts.Protocol = Proto("HTTP11") + + ht, err := New(&opts) + require.NoError(t, err) + require.Same(t, ht.client.HTTPClient, ht.client.HTTPClient2) +} + +func TestDefaultProtocolKeepsRetryableHTTP2Fallback(t *testing.T) { + opts := DefaultOptions + + ht, err := New(&opts) + require.NoError(t, err) + require.NotSame(t, ht.client.HTTPClient, ht.client.HTTPClient2) +}