Skip to content
Merged
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
32 changes: 24 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package retryablehttp
import (
"net/http"
"time"

"golang.org/x/net/http2"
)

// Client is used to make HTTP requests. It adds additional functionality
// like automatic retries to tolerate minor outages.
type Client struct {
// HTTPClient is the internal HTTP client.
// HTTPClient is the internal HTTP client (http1x + http2 via connection upgrade upgrade).
HTTPClient *http.Client
// HTTPClient is the internal HTTP client configured to fallback to native http2 at transport level
HTTPClient2 *http.Client

// RequestLogHook allows a user-supplied function to be called
// before each retry.
Expand Down Expand Up @@ -73,16 +77,22 @@ var DefaultOptionsSingle = Options{
// NewClient creates a new Client with default settings.
func NewClient(options Options) *Client {
httpclient := DefaultClient()
httpclient2 := DefaultClient()
if err := http2.ConfigureTransport(httpclient2.Transport.(*http.Transport)); err != nil {
return nil
}

// if necessary adjusts per-request timeout proportionally to general timeout (30%)
if options.Timeout > time.Second*15 {
httpclient.Timeout = time.Duration(options.Timeout.Seconds()*0.3) * time.Second
}

c := &Client{
HTTPClient: httpclient,
CheckRetry: DefaultRetryPolicy(),
Backoff: DefaultBackoff(),
options: options,
HTTPClient: httpclient,
HTTPClient2: httpclient2,
CheckRetry: DefaultRetryPolicy(),
Backoff: DefaultBackoff(),
options: options,
}

c.setKillIdleConnections()
Expand All @@ -91,10 +101,16 @@ func NewClient(options Options) *Client {

// NewWithHTTPClient creates a new Client with default settings and provided http.Client
func NewWithHTTPClient(client *http.Client, options Options) *Client {
httpclient2 := DefaultClient()
httpclient2.Transport = client.Transport.(*http.Transport).Clone()
if err := http2.ConfigureTransport(httpclient2.Transport.(*http.Transport)); err != nil {
return nil
}
c := &Client{
HTTPClient: client,
CheckRetry: DefaultRetryPolicy(),
Backoff: DefaultBackoff(),
HTTPClient: client,
HTTPClient2: httpclient2,
CheckRetry: DefaultRetryPolicy(),
Backoff: DefaultBackoff(),

options: options,
}
Expand Down
7 changes: 7 additions & 0 deletions do.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"io/ioutil"
"net/http"
"strings"
"time"
)

Expand Down Expand Up @@ -50,6 +51,12 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
// Check if we should continue with retries.
checkOK, checkErr := c.CheckRetry(req.Context(), resp, err)

// if err is equal to missing minor protocol version retry with http/2
if err != nil && strings.Contains(err.Error(), "net/http: HTTP/1.x transport connection broken: malformed HTTP version \"HTTP/2\"") {
resp, err = c.HTTPClient2.Do(req.Request)
checkOK, checkErr = c.CheckRetry(req.Context(), resp, err)
}

if err != nil {
// Increment the failure counter as the request failed
req.Metrics.Failures++
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/projectdiscovery/retryablehttp-go

go 1.14

require golang.org/x/net v0.0.0-20210521195947-fe42d452be8f
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
golang.org/x/net v0.0.0-20210521195947-fe42d452be8f h1:Si4U+UcgJzya9kpiEUJKQvjr512OLli+gL4poHrz93U=
golang.org/x/net v0.0.0-20210521195947-fe42d452be8f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=