diff --git a/pkg/util/jsonapi/client.go b/pkg/util/jsonapi/client.go index d4c86ddf0..300f9d7d9 100644 --- a/pkg/util/jsonapi/client.go +++ b/pkg/util/jsonapi/client.go @@ -16,10 +16,12 @@ package jsonapi import ( "bytes" "crypto/tls" + "errors" "fmt" "io" "net" "net/http" + "net/url" "strconv" "time" @@ -81,6 +83,17 @@ func (c *Client) do(method, path string, body []byte) (*Response, error) { } resp, err := c.client.Do(req) + if err != nil { + // check for timeout error which could be due to a failed re-used connection + var uerr *url.Error + if errors.As(err, &uerr) && uerr.Timeout() { + // close old connections + c.client.Transport.(*http.Transport).CloseIdleConnections() + + // retry request with a fresh connection + resp, err = c.client.Do(req) + } + } if err != nil { return nil, fmt.Errorf("unable to perform http request: %w", err) } @@ -108,7 +121,7 @@ func (c *Client) do(method, path string, body []byte) (*Response, error) { // NewClient returns a new HTTP client. func NewClient(host string, port uint16, tlsConfig *tls.Config) *Client { serverURL := "https://" + net.JoinHostPort(host, strconv.Itoa(int(port))) - return &Client{ + c := &Client{ client: &http.Client{ Transport: &http.Transport{TLSClientConfig: tlsConfig}, Timeout: 3 * time.Second,