Skip to content

Commit

Permalink
add default retry option to the client
Browse files Browse the repository at this point in the history
  • Loading branch information
yacut committed Apr 7, 2021
1 parent e4b94fc commit ca1a246
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 07.04.2021, Version 1.3.1

- add default retry option to the client

## 07.04.2021, Version 1.3.0

- add retry exponential backoff option
Expand Down
16 changes: 11 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ type envConfig struct {
password *string
}

func retryCondition(r *resty.Response, err error) bool {
return err != nil ||
r.StatusCode() == http.StatusTooManyRequests ||
r.StatusCode() >= http.StatusInternalServerError
}

// NewClient creates an API client using an API token
func NewClient(options ...ClientOptions) *Client {
c := Client{
Expand All @@ -53,6 +59,10 @@ func NewClient(options ...ClientOptions) *Client {
c.httpClient.SetHeader("Content-Type", "application/json")
c.httpClient.SetHeader("User-Agent", fmt.Sprintf("ilert-go/%s", Version))
c.httpClient.SetHeader("Accept-Encoding", "gzip")
c.httpClient.SetRetryCount(4).
SetRetryWaitTime(1 * time.Second).
SetRetryMaxWaitTime(5 * time.Second).
AddRetryCondition(retryCondition)

endpoint := getEnv("ILERT_ENDPOINT")
if endpoint != nil {
Expand Down Expand Up @@ -122,11 +132,7 @@ func WithRetry(retryCount int, retryWaitTime time.Duration, retryMaxWaitTime tim
SetRetryCount(retryCount).
SetRetryWaitTime(retryWaitTime).
SetRetryMaxWaitTime(retryMaxWaitTime).
AddRetryCondition(func(r *resty.Response, err error) bool {
return err != nil ||
r.StatusCode() == http.StatusTooManyRequests ||
r.StatusCode() >= http.StatusInternalServerError
})
AddRetryCondition(retryCondition)
}
}

Expand Down

0 comments on commit ca1a246

Please sign in to comment.