From 964e85c34a2c07b4ee799134b735b0c8fa898c9f Mon Sep 17 00:00:00 2001 From: kjubybot Date: Wed, 30 Mar 2022 09:33:34 +0300 Subject: [PATCH] controller: added request retry on 500 --- decort/controller.go | 45 ++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/decort/controller.go b/decort/controller.go index 5861266..addc6e3 100644 --- a/decort/controller.go +++ b/decort/controller.go @@ -27,12 +27,14 @@ package decort import ( "bytes" "crypto/tls" + "errors" "fmt" "io/ioutil" "net/http" "net/url" "strconv" "strings" + "time" // "time" @@ -378,28 +380,31 @@ func (config *ControllerCfg) decortAPICall(method string, api_name string, url_v req.Header.Set("Authorization", fmt.Sprintf("bearer %s", config.jwt)) } - resp, err := config.cc_client.Do(req) - if err != nil { - return "", err - } - defer resp.Body.Close() + for i := 0; i < 5; i++ { + resp, err := config.cc_client.Do(req) + if err != nil { + return "", err + } - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return "", err - } - log.Debugf("decortAPICall: %s %s\n %s", method, api_name, body) + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", err + } + resp.Body.Close() + log.Debugf("decortAPICall: %s %s\n %s", method, api_name, body) - if resp.StatusCode == http.StatusOK { - return string(body), nil - } else { - return "", fmt.Errorf("decortAPICall: unexpected status code %d when calling API %q with request Body %q. Respone:\n%s", - resp.StatusCode, req.URL, params_str, body) + if resp.StatusCode == http.StatusOK { + return string(body), nil + } else { + if resp.StatusCode == http.StatusInternalServerError { + log.Warnf("got 500, retrying %d/5", i+1) + time.Sleep(time.Second * 5) + continue + } + return "", fmt.Errorf("decortAPICall: unexpected status code %d when calling API %q with request Body %q. Respone:\n%s", + resp.StatusCode, req.URL, params_str, body) + } } - /* - if resp.StatusCode == StatusServiceUnavailable { - return nil, fmt.Errorf("decortAPICall method called for incompatible authorization mode %q.", config.auth_mode_txt) - } - */ + return "", errors.New("number of retries exceeded") }