From ab06c35f02827e097d0e19591d2f5c32672d7eba Mon Sep 17 00:00:00 2001 From: Miri Bar <37478344+mirii1994@users.noreply.github.com> Date: Thu, 3 Nov 2022 11:23:31 +0200 Subject: [PATCH] add retry for requests (#104) * add retry for requests * update git-secrets --- .github/workflows/git-secrets.yml | 5 +-- .gitignore | 1 + README.md | 8 +++-- go.mod | 2 ++ go.sum | 2 ++ utils.go | 54 +++++++++++++++++++++++-------- 6 files changed, 54 insertions(+), 18 deletions(-) diff --git a/.github/workflows/git-secrets.yml b/.github/workflows/git-secrets.yml index b83827e..c94768c 100644 --- a/.github/workflows/git-secrets.yml +++ b/.github/workflows/git-secrets.yml @@ -11,9 +11,9 @@ jobs: # Steps represent a sequence of tasks that will be executed as part of the job steps: - name: Check Out Source Code - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Set up Python 3.8 - uses: actions/setup-python@v2 + uses: actions/setup-python@v4 with: python-version: 3.8 - name: Installing dependencies @@ -22,6 +22,7 @@ jobs: - name: Installing scanning tool run: | eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" + ln -s "$(which echo)" /usr/local/bin/say brew install git-secrets git secrets --install git secrets --register-aws diff --git a/.gitignore b/.gitignore index 79efff6..25abf41 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ vendor # swagger server swagger +.DS_store \ No newline at end of file diff --git a/README.md b/README.md index 4f4512c..6aacd00 100644 --- a/README.md +++ b/README.md @@ -30,18 +30,20 @@ The library currently supports the following API endpoints: ### Changelog +- v1.13.1 + - Add retry mechanism for requests. - v1.13.0 - Bug fix - **sub_accounts**: field `ReservedDailyGB` in requests can be 0. - v1.12.0 - Upgrade to Go 1.18. - Refactor `users`, adjust to the recent API fields. - Add field `UserName` to `restore` initiate request, to match recent API fields. -- v1.11.0 - - Add [Kibana Objects](https://docs.logz.io/api/#tag/Import-or-export-Kibana-objects). +
Exapnd to check old versions - +- v1.11.0 + - Add [Kibana Objects](https://docs.logz.io/api/#tag/Import-or-export-Kibana-objects). - v1.10.3 - Bug fix - **sub_accounts**: omit maxDailyGb if needed. - v1.10.2 diff --git a/go.mod b/go.mod index ca757a9..247f4f9 100644 --- a/go.mod +++ b/go.mod @@ -5,9 +5,11 @@ go 1.18 require ( github.com/hashicorp/go-hclog v0.15.0 github.com/stretchr/testify v1.3.0 + ) require ( + github.com/avast/retry-go v3.0.0+incompatible // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/fatih/color v1.7.0 // indirect github.com/mattn/go-colorable v0.1.4 // indirect diff --git a/go.sum b/go.sum index 6b1e7f3..762fbf2 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/avast/retry-go v3.0.0+incompatible h1:4SOWQ7Qs+oroOTQOYnAHqelpCO0biHSxpiH9JdtuBj0= +github.com/avast/retry-go v3.0.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/utils.go b/utils.go index c337280..04d9cd3 100644 --- a/utils.go +++ b/utils.go @@ -4,9 +4,11 @@ import ( "bytes" "encoding/json" "fmt" + "github.com/avast/retry-go" "github.com/logzio/logzio_terraform_client/client" "io/ioutil" "net/http" + "strings" ) const ( @@ -89,21 +91,47 @@ func CallLogzioApi(logzioCall LogzioApiCallDetails) ([]byte, error) { } httpClient := client.GetHttpClient(req) - resp, err := httpClient.Do(req) - if err != nil { - return nil, err - } + var resp *http.Response + var jsonBytes []byte + + err = retry.Do( + func() error { + resp, err = httpClient.Do(req) + if err != nil { + return err + } + + jsonBytes, _ = ioutil.ReadAll(resp.Body) + if !CheckValidStatus(resp, logzioCall.SuccessCodes) { + if resp.StatusCode == logzioCall.NotFoundCode { + return fmt.Errorf("API call %s failed with missing %s %d, data: %s", + logzioCall.ApiAction, logzioCall.ResourceName, logzioCall.ResourceId, jsonBytes) + } + + return fmt.Errorf("API call %s failed with status code %d, data: %s", + logzioCall.ApiAction, resp.StatusCode, jsonBytes) + } + + return nil + }, + retry.RetryIf( + func(err error) bool { + if err != nil { + if strings.Contains(err.Error(), "status code 429") || + strings.Contains(err.Error(), "failed with missing") || + strings.Contains(err.Error(), "status code 500") { + return true + } + } + return false + }), + retry.DelayType(retry.BackOffDelay), + retry.Attempts(8), + ) defer resp.Body.Close() - jsonBytes, _ := ioutil.ReadAll(resp.Body) - if !CheckValidStatus(resp, logzioCall.SuccessCodes) { - if resp.StatusCode == logzioCall.NotFoundCode { - return nil, fmt.Errorf("API call %s failed with missing %s %d, data: %s", - logzioCall.ApiAction, logzioCall.ResourceName, logzioCall.ResourceId, jsonBytes) - } - - return nil, fmt.Errorf("API call %s failed with status code %d, data: %s", - logzioCall.ApiAction, resp.StatusCode, jsonBytes) + if err != nil { + return nil, err } return jsonBytes, nil