Skip to content

Commit

Permalink
- add retry exponential backoff option
Browse files Browse the repository at this point in the history
- upgrade to golang 1.6
  • Loading branch information
yacut committed Apr 7, 2021
1 parent 2deb67c commit e4b94fc
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 07.04.2021, Version 1.3.0

- add retry exponential backoff option
- upgrade to golang 1.6

## 12.03.2021, Version 1.2.2

- fix team visibility
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (

func main() {

client := ilert.NewClient()
// We strongly recommend to enable a retry logic if an error occurs
client := ilert.NewClient(ilert.WithRetry(10, 5*time.Second, 20*time.Second))

var apiKey = "alert source API Key"
event := &ilert.Event{
Expand Down
24 changes: 23 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ilert
import (
"encoding/json"
"fmt"
"net/http"
"os"
"time"

Expand Down Expand Up @@ -108,9 +109,30 @@ func WithUserAgent(agent string) ClientOptions {
}
}

// WithRetry enables retry logic with exponential backoff for the following errors:
//
// - any network errors
//
// - 5xx errors: this indicates an error in iLert
//
// - 429 Too Many Requests: you have reached your rate limit
func WithRetry(retryCount int, retryWaitTime time.Duration, retryMaxWaitTime time.Duration) ClientOptions {
return func(c *Client) {
c.httpClient.
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
})
}
}

func catchGenericAPIError(response *resty.Response, expectedStatusCode ...int) error {
if !intSliceContains(expectedStatusCode, response.StatusCode()) {
restErr := fmt.Errorf("Wrong status code %d", response.StatusCode())
restErr := fmt.Errorf("wrong status code %d", response.StatusCode())
respBody := &GenericErrorResponse{}
err := json.Unmarshal(response.Body(), respBody)
if err == nil && respBody.Message != "" {
Expand Down
3 changes: 2 additions & 1 deletion examples/event/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"time"

"github.com/iLert/ilert-go"
)
Expand All @@ -15,7 +16,7 @@ func main() {
IncidentKey: "123456",
}
input := &ilert.CreateEventInput{Event: event}
client := ilert.NewClient()
client := ilert.NewClient(ilert.WithRetry(10, 5*time.Second, 20*time.Second))
result, err := client.CreateEvent(input)
if err != nil {
log.Println(result)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/iLert/ilert-go

go 1.15
go 1.16

require github.com/go-resty/resty/v2 v2.3.0
require github.com/go-resty/resty/v2 v2.5.0
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/go-resty/resty/v2 v2.3.0 h1:JOOeAvjSlapTT92p8xiS19Zxev1neGikoHsXJeOq8So=
github.com/go-resty/resty/v2 v2.3.0/go.mod h1:UpN9CgLZNsv4e9XG50UU8xdI0F43UQ4HmxLBDwaroHU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200513185701-a91f0712d120 h1:EZ3cVSzKOlJxAd8e8YAJ7no8nNypTxexh/YE/xW3ZEY=
golang.org/x/net v0.0.0-20200513185701-a91f0712d120/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
github.com/go-resty/resty/v2 v2.5.0 h1:WFb5bD49/85PO7WgAjZ+/TJQ+Ty1XOcWEfD1zIFCM1c=
github.com/go-resty/resty/v2 v2.5.0/go.mod h1:B88+xCTEwvfD94NOuE6GS1wMlnoKNY8eEiNizfNwOwA=
golang.org/x/net v0.0.0-20201224014010-6772e930b67b h1:iFwSg7t5GZmB/Q5TjiEAsdoLDrdJRC1RiF2WhuV29Qw=
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

0 comments on commit e4b94fc

Please sign in to comment.