controller: added request retry on 500

2.1
kjubybot 3 years ago
parent 94f222fe19
commit 964e85c34a

@ -27,12 +27,14 @@ package decort
import ( import (
"bytes" "bytes"
"crypto/tls" "crypto/tls"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
"strings" "strings"
"time"
// "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)) req.Header.Set("Authorization", fmt.Sprintf("bearer %s", config.jwt))
} }
resp, err := config.cc_client.Do(req) for i := 0; i < 5; i++ {
if err != nil { resp, err := config.cc_client.Do(req)
return "", err if err != nil {
} return "", err
defer resp.Body.Close() }
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return "", err return "", err
} }
log.Debugf("decortAPICall: %s %s\n %s", method, api_name, body) resp.Body.Close()
log.Debugf("decortAPICall: %s %s\n %s", method, api_name, body)
if resp.StatusCode == http.StatusOK { if resp.StatusCode == http.StatusOK {
return string(body), nil return string(body), nil
} else { } else {
return "", fmt.Errorf("decortAPICall: unexpected status code %d when calling API %q with request Body %q. Respone:\n%s", if resp.StatusCode == http.StatusInternalServerError {
resp.StatusCode, req.URL, params_str, body) 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)
}
} }
/* return "", errors.New("number of retries exceeded")
if resp.StatusCode == StatusServiceUnavailable {
return nil, fmt.Errorf("decortAPICall method called for incompatible authorization mode %q.", config.auth_mode_txt)
}
*/
} }

Loading…
Cancel
Save