controller: added request retry on 500
This commit is contained in:
@@ -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))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
resp, err := config.cc_client.Do(req)
|
resp, err := config.cc_client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
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
|
||||||
}
|
}
|
||||||
|
resp.Body.Close()
|
||||||
log.Debugf("decortAPICall: %s %s\n %s", method, api_name, body)
|
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 {
|
||||||
|
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",
|
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)
|
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)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user