Add retry mechanism.

This commit is contained in:
2023-04-07 15:45:43 +01:00
parent e0913f8824
commit 2a628a7da7
6 changed files with 52 additions and 20 deletions
+31 -16
View File
@@ -5,6 +5,8 @@ import (
"fmt"
"io/ioutil"
"net/http"
"github.com/avast/retry-go/v4"
)
func (c *BaseClient) executeQuery(query []byte, headers any) (result any, err error) {
@@ -21,22 +23,35 @@ func (c *BaseClient) executeQuery(query []byte, headers any) (result any, err er
var httpResponse *http.Response
httpResponse, err = c.client.Do(httpRequest)
if err != nil {
c.Logger.Error(c, "Error while executing http request;", "error", err.Error())
return
}
defer httpResponse.Body.Close()
body, err := ioutil.ReadAll(httpResponse.Body)
if err != nil {
c.Logger.Error(c, "Error while reading http response;", "error", err.Error())
return
}
err = json.Unmarshal(body, &queryResult)
if err != nil {
c.Logger.Error(c, "Error while unmarshalling http response;", "error", err.Error())
return
}
err = retry.Do(
func() error {
httpResponse, err = c.client.Do(httpRequest)
if err != nil {
c.Logger.Error(c, "Error while executing http request;", "error", err.Error())
return err
}
defer httpResponse.Body.Close()
if httpResponse.StatusCode <= 200 && httpResponse.StatusCode >= 204 {
return err
}
body, err := ioutil.ReadAll(httpResponse.Body)
if err != nil {
c.Logger.Error(c, "Error while reading http response;", "error", err.Error())
return err
}
err = json.Unmarshal(body, &queryResult)
if err != nil {
c.Logger.Error(c, "Error while unmarshalling http response;", "error", err.Error())
return err
}
return nil
},
)
if len(queryResult.Errors) > 0 {
return nil, fmt.Errorf("%v", queryResult.Errors)
}