-
Notifications
You must be signed in to change notification settings - Fork 5
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
CLOUDP-237245: Add example for retrying requests #294
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
fbf8e64
CLOUDP-237245: retry example
wtrocki f37e8ea
fix: extract function
wtrocki 0f2d20a
fix: update package and use helper
wtrocki c8ceaef
fix: separate go mod for examples
wtrocki 12f3972
fix: ignore dependabot for examples
wtrocki c61e96e
fix: review comments
wtrocki 82401e4
fix: whitespace
wtrocki fba8682
fix: retry function
wtrocki f8817f1
fix: update and review golang ci lint rules
wtrocki 1cd3ad6
fix: update golang ci lint
wtrocki cc4c520
chore: update readme
wtrocki c22662b
Update examples/retry/retry.go
wtrocki d5d4d9a
Update examples/retry/retry.go
wtrocki bc86dce
Update examples/go.mod
wtrocki 51d06d8
Update examples/retry/retry.go
wtrocki c1df4dd
fix: retry options
wtrocki bf10f2e
fix: remove deprecated configs for linter
wtrocki 5193787
fix: use non released version of the SDK in examples
wtrocki 0ca7eae
fix: bring back testify
wtrocki File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
golang 1.20 | ||
golang 1.22.1 | ||
nodejs 18.18.2 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module go.mongodb.org/atlas-sdk/v20231115008/examples | ||
|
||
go 1.22.1 | ||
|
||
replace go.mongodb.org/atlas-sdk/v20231115008 => ../ | ||
lantoli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
require ( | ||
github.com/hashicorp/go-retryablehttp v0.7.5 | ||
github.com/mongodb-forks/digest v1.1.0 | ||
go.mongodb.org/atlas-sdk/v20231115008 v20231115008.1.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/stretchr/objx v0.5.2 // indirect | ||
github.com/stretchr/testify v1.9.0 | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
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= | ||
github.com/go-test/deep v1.1.0/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE= | ||
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= | ||
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= | ||
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ= | ||
github.com/hashicorp/go-retryablehttp v0.7.5 h1:bJj+Pj19UZMIweq/iie+1u5YCdGrnxCT9yvm0e+Nd5M= | ||
github.com/hashicorp/go-retryablehttp v0.7.5/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8= | ||
github.com/mongodb-forks/digest v1.1.0 h1:7eUdsR1BtqLv0mdNm4OXs6ddWvR4X2/OsLwdKksrOoc= | ||
github.com/mongodb-forks/digest v1.1.0/go.mod h1:rb+EX8zotClD5Dj4NdgxnJXG9nwrlx3NWKJ8xttz1Dg= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= | ||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= | ||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= | ||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
go.mongodb.org/atlas-sdk/v20231115008 v20231115008.1.0 h1:WKPkreeCuIh2KKxfEP6KWIFNdNYaXnfSg79v2zAaSjw= | ||
go.mongodb.org/atlas-sdk/v20231115008 v20231115008.1.0/go.mod h1:BHskDmYvvANe+s/HMkRhvK3GudGEZuzjYSbp1fBssdc= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
|
||
"context" | ||
|
||
"go.mongodb.org/atlas-sdk/v20231115008/admin" | ||
|
||
retryablehttp "github.com/hashicorp/go-retryablehttp" | ||
"github.com/mongodb-forks/digest" | ||
) | ||
|
||
/* | ||
* MongoDB Atlas Go SDK Retryable Request Example | ||
* | ||
* Example using custom http client that handles rate limiting and 500 Http errors by retrying requests automatically. | ||
* Example uses https://pkg.go.dev/github.com/hashicorp/go-retryablehttp. | ||
* Please refer to the package documentation for more information. | ||
*/ | ||
func main() { | ||
ctx := context.Background() | ||
// Values provided as part of env variables | ||
// See: https://www.mongodb.com/docs/atlas/app-services/authentication/api-key/ | ||
apiKey := os.Getenv("MONGODB_ATLAS_PUBLIC_KEY") | ||
apiSecret := os.Getenv("MONGODB_ATLAS_PRIVATE_KEY") | ||
url := os.Getenv("MONGODB_ATLAS_URL") | ||
|
||
// Using custom client | ||
// This example relies on https://pkg.go.dev/github.com/hashicorp/go-retryablehttp | ||
// retryablehttp performs automatic retries under certain conditions. | ||
// Mainly, if an error is returned by the client (connection errors etc), | ||
/// or if a 500-range response is received, then a retry is invoked. | ||
retryClient := retryablehttp.NewClient() | ||
retryClient.RetryMax = 3 | ||
|
||
retryableClient, err := newRetryableClient(retryClient, apiKey, apiSecret) | ||
if err != nil { | ||
log.Fatal("Cannot instantiate client") | ||
} | ||
sdk, err := admin.NewClient( | ||
admin.UseHTTPClient(retryableClient), | ||
admin.UseBaseURL(url), | ||
admin.UseDebug(false)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
request := sdk.ProjectsApi.ListProjectsWithParams(ctx, | ||
&admin.ListProjectsApiParams{ | ||
ItemsPerPage: admin.PtrInt(1), | ||
IncludeCount: admin.PtrBool(true), | ||
PageNum: admin.PtrInt(1), | ||
}) | ||
projects, _, err := request.Execute() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Println("Total Projects", projects.GetTotalCount()) | ||
|
||
} | ||
|
||
func newRetryableClient(retryClient *retryablehttp.Client, apiKey string, apiSecret string) (*http.Client, error) { | ||
var transport http.RoundTripper = &retryablehttp.RoundTripper{Client: retryClient} | ||
digestRetryAbleTransport := digest.NewTransportWithHTTPRoundTripper(apiKey, apiSecret, transport) | ||
return digestRetryAbleTransport.Client() | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is testify still needed here, could we There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do:
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wtrocki @gssbzn note that for instance cluster creation can throw 500 but still created the cluster so next call will be DUPLICATE_CLUSTER_NAME 400 that will be a bit confusing for clients.
retries are mostly useful when operations are atomic (happen completely or don't happen at all)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, personally blindly retrying on the Atlas API is not safe and a bad idea