Skip to content
This repository was archived by the owner on Jun 17, 2022. It is now read-only.

Commit f0829cc

Browse files
authored
retry only 5xx errors. Fixes #349 (#354)
1 parent d9058bc commit f0829cc

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

gcp/http.go

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,26 @@ func newClient(oauthClientID, oauthClientSecret, oauthAuthURL, oauthTokenURL, re
6060
return client, nil
6161
}
6262

63-
// getWithRetry calls get() and retries on HTTP failure
64-
// (response code != 200).
63+
// getWithRetry calls get() and retries on HTTP temp failure
64+
// (response code 500-599).
6565
func getWithRetry(hc *http.Client, url string) (*http.Response, error) {
6666
backoff := lib.Backoff{}
6767
for {
6868
response, err := get(hc, url)
6969
if response != nil && response.StatusCode == http.StatusOK {
7070
return response, err
71-
}
72-
73-
p, retryAgain := backoff.Pause()
74-
if !retryAgain {
75-
log.Debugf("HTTP error %s, retry timeout hit", err, p)
71+
} else if response != nil && response.StatusCode >= 500 && response.StatusCode <= 599 {
72+
p, retryAgain := backoff.Pause()
73+
if !retryAgain {
74+
log.Debugf("HTTP error %s, retry timeout hit", err)
75+
return response, err
76+
}
77+
log.Debugf("HTTP error %s, retrying after %s", err, p)
78+
time.Sleep(p)
79+
} else {
80+
log.Debugf("Permanent HTTP error %s, will not retry", err)
7681
return response, err
7782
}
78-
log.Debugf("HTTP error %s, retrying after %s", err, p)
79-
time.Sleep(p)
8083
}
8184
}
8285

@@ -95,32 +98,35 @@ func get(hc *http.Client, url string) (*http.Response, error) {
9598
response, err := hc.Do(request)
9699
lock.Release()
97100
if err != nil {
98-
return nil, fmt.Errorf("GET failure: %s", err)
101+
return response, fmt.Errorf("GET failure: %s", err)
99102
}
100103
if response.StatusCode != http.StatusOK {
101-
return nil, fmt.Errorf("GET HTTP-level failure: %s %s", url, response.Status)
104+
return response, fmt.Errorf("GET HTTP-level failure: %s %s", url, response.Status)
102105
}
103106

104107
return response, nil
105108
}
106109

107-
// postWithRetry calls post() and retries on HTTP failure
108-
// (response code != 200).
110+
// postWithRetry calls post() and retries on HTTP temp failure
111+
// (response code 500-599).
109112
func postWithRetry(hc *http.Client, url string, form url.Values) ([]byte, uint, int, error) {
110113
backoff := lib.Backoff{}
111114
for {
112115
responseBody, gcpErrorCode, httpStatusCode, err := post(hc, url, form)
113116
if responseBody != nil && httpStatusCode == http.StatusOK {
114117
return responseBody, gcpErrorCode, httpStatusCode, err
115-
}
116-
117-
p, retryAgain := backoff.Pause()
118-
if !retryAgain {
119-
log.Debugf("HTTP error %s, retry timeout hit", err, p)
118+
} else if responseBody != nil && httpStatusCode >= 500 && httpStatusCode <= 599 {
119+
p, retryAgain := backoff.Pause()
120+
if !retryAgain {
121+
log.Debugf("HTTP error %s, retry timeout hit", err)
122+
return responseBody, gcpErrorCode, httpStatusCode, err
123+
}
124+
log.Debugf("HTTP error %s, retrying after %s", err, p)
125+
time.Sleep(p)
126+
} else {
127+
log.Debugf("Permanent HTTP error %s, will not retry", err)
120128
return responseBody, gcpErrorCode, httpStatusCode, err
121129
}
122-
log.Debugf("HTTP error %s, retrying after %s", err, p)
123-
time.Sleep(p)
124130
}
125131
}
126132

0 commit comments

Comments
 (0)