Skip to content

Commit

Permalink
add retry for requests (#104)
Browse files Browse the repository at this point in the history
* add retry for requests

* update git-secrets
  • Loading branch information
mirii1994 authored Nov 3, 2022
1 parent c7d8ca4 commit ab06c35
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 18 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/git-secrets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ vendor
# swagger server
swagger

.DS_store
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).


<details>
<summary markdown="span">Exapnd to check old versions </summary>

- 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
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
54 changes: 41 additions & 13 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ab06c35

Please sign in to comment.