Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retry logic to modules #147

Merged
merged 11 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# Release (Unreleased)

* `github.com/patrickcping/pingone-go-sdk-v2` : v0.6.3
* **Note** Add retry logic for retryable HTTP status codes. [#147](https://github.com/patrickcping/pingone-go-sdk-v2/pull/147)
* `github.com/patrickcping/pingone-go-sdk-v2/agreementmanagement` : [v0.1.2](./agreementmanagement/CHANGELOG.md)
* **Note** Add retry logic for retryable HTTP status codes. [#147](https://github.com/patrickcping/pingone-go-sdk-v2/pull/147)
* `github.com/patrickcping/pingone-go-sdk-v2/authorize` : [v0.1.5](./authorize/CHANGELOG.md)
* **Note** Add retry logic for retryable HTTP status codes. [#147](https://github.com/patrickcping/pingone-go-sdk-v2/pull/147)
* `github.com/patrickcping/pingone-go-sdk-v2/management` : [v0.18.1](./management/CHANGELOG.md)
* **Note** Add retry logic for retryable HTTP status codes. [#147](https://github.com/patrickcping/pingone-go-sdk-v2/pull/147)
* `github.com/patrickcping/pingone-go-sdk-v2/mfa` : [v0.9.2](./mfa/CHANGELOG.md)
* **Note** Add retry logic for retryable HTTP status codes. [#147](https://github.com/patrickcping/pingone-go-sdk-v2/pull/147)
* `github.com/patrickcping/pingone-go-sdk-v2/risk` : [v0.4.1](./risk/CHANGELOG.md)
* **Note** Add retry logic for retryable HTTP status codes. [#147](https://github.com/patrickcping/pingone-go-sdk-v2/pull/147)

# Release (2023-04-18)

* `github.com/patrickcping/pingone-go-sdk-v2` : v0.6.2
Expand Down
2 changes: 1 addition & 1 deletion agreementmanagement/.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.1
0.1.2
4 changes: 4 additions & 0 deletions agreementmanagement/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v0.1.2 (Unreleased)

* **Note** Add retry logic for retryable HTTP status codes. [#147](https://github.com/patrickcping/pingone-go-sdk-v2/pull/147)

# v0.1.1 (2023-03-20)

* **Note** Adjust client request/response debug and fix linting issues. [#133](https://github.com/patrickcping/pingone-go-sdk-v2/pull/133)
Expand Down
2 changes: 1 addition & 1 deletion agreementmanagement/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The PingOne Platform API covering the PingOne Agreement Management service
This API client was generated by the [OpenAPI Generator](https://openapi-generator.tech) project. By using the [OpenAPI-spec](https://www.openapis.org/) from a remote server, you can easily generate an API client.

- API version: 2022-09-23
- Package version: 0.1.1
- Package version: 0.1.2
- Build package: org.openapitools.codegen.languages.GoClientCodegen

## Installation
Expand Down
9 changes: 9 additions & 0 deletions agreementmanagement/api_agreement_revisions_resources.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

119 changes: 119 additions & 0 deletions agreementmanagement/client_ext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package agreementmanagement


import (
"fmt"
"log"
"net/http"
"strconv"
"time"

"golang.org/x/exp/slices"
)

type SDKInterfaceFunc func() (interface{}, *http.Response, error)

var (
maxRetries = 5
maximumRetryAfterBackoff = 30
)

func processResponse(f SDKInterfaceFunc) (interface{}, *http.Response, error) {
obj, response, error := exponentialBackOffRetry(f)
return obj, response, reformError(error)
}

func reformError(err error) error {
return err
}

func exponentialBackOffRetry(f SDKInterfaceFunc) (interface{}, *http.Response, error) {
var obj interface{}
var resp *http.Response
var err error
backOffTime := time.Second
var isRetryable bool

for i := 0; i < maxRetries; i++ {
obj, resp, err = f()

backOffTime, isRetryable = testForRetryable(resp, err, backOffTime)

if isRetryable {
log.Printf("Attempt %d failed: %v, backing off by %s.", i+1, err, backOffTime.String())
time.Sleep(backOffTime)
continue
}

return obj, resp, err
}

log.Printf("Request failed after %d attempts", maxRetries)

return obj, resp, err // output the final error
}

func testForRetryable(r *http.Response, err error, currentBackoff time.Duration) (time.Duration, bool) {

backoff := currentBackoff

if r != nil {
if r.StatusCode == 501 || r.StatusCode == 503 || r.StatusCode == 429 {
retryAfter, err := parseRetryAfterHeader(r)
if err != nil {
log.Printf("Cannot parse the expected \"Retry-After\" header: %s", err)
backoff = currentBackoff * 2
}

if retryAfter <= time.Duration(maximumRetryAfterBackoff) {
backoff += time.Duration(maximumRetryAfterBackoff)
} else {
backoff += retryAfter
}
} else {
backoff = currentBackoff * 2
}

retryAbleCodes := []int{
429,
500,
501,
502,
503,
504,
}

if slices.Contains(retryAbleCodes, r.StatusCode) {
log.Printf("HTTP status code %d detected, available for retry", r.StatusCode)
return backoff, true
}
}

if err != nil {
log.Printf("Error found but not retried: %s", err)
}

return backoff, false
}

func parseRetryAfterHeader(resp *http.Response) (time.Duration, error) {
retryAfterHeader := resp.Header.Get("Retry-After")

if retryAfterHeader == "" {
return 0, fmt.Errorf("Retry-After header not found")
}

retryAfterSeconds, err := strconv.Atoi(retryAfterHeader)

if err == nil {
return time.Duration(retryAfterSeconds) * time.Second, nil
}

retryAfterTime, err := http.ParseTime(retryAfterHeader)

if err != nil {
return 0, fmt.Errorf("Unable to parse Retry-After header value: %v", err)
}

return time.Until(retryAfterTime), nil
}
2 changes: 1 addition & 1 deletion agreementmanagement/configuration.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions agreementmanagement/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/patrickcping/pingone-go-sdk-v2/agreementmanagement

go 1.18

require golang.org/x/exp v0.0.0-20230321023759-10a507213a29
2 changes: 2 additions & 0 deletions agreementmanagement/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
2 changes: 1 addition & 1 deletion authorize/.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.4
0.1.5
4 changes: 4 additions & 0 deletions authorize/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v0.1.5 (Unreleased)

* **Note** Add retry logic for retryable HTTP status codes. [#147](https://github.com/patrickcping/pingone-go-sdk-v2/pull/147)

# v0.1.4 (2023-03-20)

* **Note** Adjust client request/response debug and fix linting issues. [#133](https://github.com/patrickcping/pingone-go-sdk-v2/pull/133)
Expand Down
2 changes: 1 addition & 1 deletion authorize/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The PingOne Platform API covering the PingOne Authorize service
This API client was generated by the [OpenAPI Generator](https://openapi-generator.tech) project. By using the [OpenAPI-spec](https://www.openapis.org/) from a remote server, you can easily generate an API client.

- API version: 2022-09-23
- Package version: 0.1.4
- Package version: 0.1.5
- Build package: org.openapitools.codegen.languages.GoClientCodegen

## Installation
Expand Down
46 changes: 46 additions & 0 deletions authorize/api_api_servers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions authorize/api_policy_decision_management.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading