Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions common/httpx/httpx.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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{}
}

Expand All @@ -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{
Expand Down Expand Up @@ -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()
Expand Down
26 changes: 26 additions & 0 deletions common/httpx/httpx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}