From 3731967dc4ca605104a2d63913e5287921ec8ef8 Mon Sep 17 00:00:00 2001 From: ale <ale@incal.net> Date: Mon, 27 Nov 2017 08:45:02 +0000 Subject: [PATCH] Retry HTTP requests on bad gateways and code 429 Code 429 is used by the server-side throttling code. --- clientutil/retry.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/clientutil/retry.go b/clientutil/retry.go index ae5159f..054d301 100644 --- a/clientutil/retry.go +++ b/clientutil/retry.go @@ -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 } -- GitLab