diff --git a/src/httpify/client.go b/src/httpify/client.go index cc836854..d2c9ef09 100644 --- a/src/httpify/client.go +++ b/src/httpify/client.go @@ -7,7 +7,6 @@ import ( "errors" "fmt" "io/ioutil" - "net" "net/http" "net/url" "runtime" @@ -119,20 +118,23 @@ func (client *HTTPClient) doWithRetry(req *http.Request, body interface{}) (*htt client.limit.Wait() resp, err := client.client.Do(req) - if err == nil && resp.StatusCode >= 100 && resp.StatusCode <= 428 { - return resp, nil - } else if err, ok := err.(net.Error); ok && err.Timeout() { - attempt++ - if attempt > client.maxRetry { - return resp, fmt.Errorf("request timed out after %v retries, there may be an issue with your connection", client.maxRetry) + if err == nil { + if resp.StatusCode >= 100 && resp.StatusCode <= 428 { + return resp, nil + } else if resp.StatusCode == http.StatusTooManyRequests { + after, _ := strconv.ParseFloat(resp.Header.Get("Retry-After"), 10) + client.limit.ResetAfter(time.Duration(after)) + continue } - time.Sleep(time.Duration(attempt) * time.Second) - } else if resp.StatusCode == http.StatusTooManyRequests { - after, _ := strconv.ParseFloat(resp.Header.Get("Retry-After"), 10) - client.limit.ResetAfter(time.Duration(after)) - } else if err != nil && strings.Contains(err.Error(), "no such host") { + } else if strings.Contains(err.Error(), "no such host") { return nil, errConnectionIssue } + + attempt++ + if attempt > client.maxRetry { + return resp, fmt.Errorf("request failed after %v retries with err: %v", client.maxRetry, err) + } + time.Sleep(time.Duration(attempt) * time.Second) } } diff --git a/src/httpify/client_test.go b/src/httpify/client_test.go index 6efd09e4..55ceffad 100644 --- a/src/httpify/client_test.go +++ b/src/httpify/client_test.go @@ -105,7 +105,7 @@ func TestClient_do(t *testing.T) { if request == 0 { request++ mut.Unlock() - time.Sleep(time.Second) + time.Sleep(5 * time.Millisecond) } else { mut.Unlock() } @@ -139,7 +139,7 @@ func TestClient_do(t *testing.T) { assert.Nil(t, err) _, err = client.do("POST", "/assets.json", body, nil) - assert.EqualError(t, err, "request timed out after 1 retries, there may be an issue with your connection") + assert.Contains(t, err.Error(), "request failed after 1 retries with err: ", server.URL) server.Close() }