Skip to content
Snippets Groups Projects
Commit 3731967d authored by ale's avatar ale
Browse files

Retry HTTP requests on bad gateways and code 429

Code 429 is used by the server-side throttling code.
parent f8e1dddc
No related branches found
No related tags found
No related merge requests found
......@@ -33,12 +33,20 @@ func Retry(op backoff.Operation, b backoff.BackOff) error {
return backoff.Retry(innerOp, b)
}
var errHTTPBackOff = errors.New("http status 503")
var errHTTPBackOff = errors.New("temporary http error")
func isStatusTemporary(code int) bool {
switch code {
case http.StatusTooManyRequests, http.StatusBadGateway, http.StatusServiceUnavailable, http.StatusGatewayTimeout:
return true
default:
return false
}
}
// RetryHTTPDo retries an HTTP request until it succeeds, according to
// the backoff policy b. It will retry on temporary network errors and
// upon receiving specific throttling HTTP errors (currently just
// status code 503).
// upon receiving specific temporary HTTP errors.
func RetryHTTPDo(client *http.Client, req *http.Request, b backoff.BackOff) (*http.Response, error) {
var resp *http.Response
op := func() error {
......@@ -49,7 +57,7 @@ func RetryHTTPDo(client *http.Client, req *http.Request, b backoff.BackOff) (*ht
var err error
resp, err = client.Do(req)
if err == nil && resp.StatusCode == 503 {
if err == nil && isStatusTemporary(resp.StatusCode) {
resp.Body.Close()
return errHTTPBackOff
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment