Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing hanging uploads #809

Merged
merged 2 commits into from
Sep 29, 2020
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
26 changes: 14 additions & 12 deletions src/httpify/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"runtime"
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with error ?

}
time.Sleep(time.Duration(attempt) * time.Second)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/httpify/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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()
}

Expand Down