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

CLOUDP-237245: Add example for retrying requests #294

Merged
merged 19 commits into from Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions examples/README.md
@@ -1,4 +1,4 @@
## Go SDK examples
# Go SDK examples

## Running Examples

Expand All @@ -11,7 +11,6 @@ export MONGODB_ATLAS_PRIVATE_KEY=some-secret-key-for-gosdkapi
go run ./aws_cluster/aws.go
```


## Running Examples with Mocked Backend

SDK provides mocks using Testify and Mockery.
Expand All @@ -20,3 +19,11 @@ One of the SDK examples covers usage of the mockery within tests.
```bash
go test ./mock/cluster_test.go
```

## Examples Reference

### Retry Example

Example provides automatic retries for all HTTP 500, 429 HTTP status errors.
Copy link
Member

@lantoli lantoli Apr 1, 2024

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)

Copy link
Collaborator

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


`go run ./retry/retry.go`
wtrocki marked this conversation as resolved.
Show resolved Hide resolved
74 changes: 74 additions & 0 deletions examples/retry/retry.go
@@ -0,0 +1,74 @@
package main

import (
"log"
"net/http"
"os"

"context"

"go.mongodb.org/atlas-sdk/v20231115008/admin"
"go.mongodb.org/atlas-sdk/v20231115008/examples"

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 := createRetryableClient(retryClient, apiKey, apiSecret)
sdk, err := admin.NewClient(
admin.UseHTTPClient(retryableClient),
admin.UseBaseURL(url),
admin.UseDebug(false))
examples.HandleErr(err, nil)

request := sdk.ProjectsApi.ListProjectsWithParams(ctx,
&admin.ListProjectsApiParams{
ItemsPerPage: admin.PtrInt(1),
IncludeCount: admin.PtrBool(true),
PageNum: admin.PtrInt(1),
})
projects, response, err := request.Execute()
examples.HandleErr(err, response)

if projects.GetTotalCount() == 0 {
log.Fatal("account should have at least single project")
}
wtrocki marked this conversation as resolved.
Show resolved Hide resolved

}

func createRetryableClient(retryClient *retryablehttp.Client, apiKey string, apiSecret string) *http.Client {
wtrocki marked this conversation as resolved.
Show resolved Hide resolved
var transport http.RoundTripper = &retryablehttp.RoundTripper{Client: retryClient}
digestRetryAbleTransport := &digest.Transport{
Username: apiKey,
Password: apiSecret,
Transport: transport,
}
retryableClient, err := digestRetryAbleTransport.Client()
if err != nil {
log.Fatal("Cannot instantiate client")
}
wtrocki marked this conversation as resolved.
Show resolved Hide resolved
return retryableClient
}
5 changes: 5 additions & 0 deletions go.mod
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is testify still needed here, could we go mod tidy?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do:

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

Expand Up @@ -7,6 +7,11 @@ require (
github.com/mongodb-forks/digest v1.0.5
)

require (
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.5 // indirect
)
wtrocki marked this conversation as resolved.
Show resolved Hide resolved

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Expand Up @@ -2,12 +2,18 @@ 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 h1:WOcxcdHcvdgThNXjw0t76K42FXTU7HpNQWHpA2HHNlg=
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.0.5 h1:EJu3wtLZcA0HCvsZpX5yuD193/sW9tHiNvrEM5apXMk=
github.com/mongodb-forks/digest v1.0.5/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 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
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=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
28 changes: 28 additions & 0 deletions internal/main.go
wtrocki marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,28 @@
package main

import (
"encoding/json"
"fmt"

"go.mongodb.org/atlas-sdk/v20231115008/admin"
)

func main() {
var num int = 0
myStruct := admin.DiskBackupSnapshotSchedule{
ReferenceHourOfDay:&num,
}

// Print the struct before encoding to JSON
fmt.Println("Before encoding:", myStruct)

// Encode struct to JSON
encoded, err := json.Marshal(myStruct)
if err != nil {
fmt.Println("Error encoding JSON:", err)
return
}

// Print the encoded JSON
fmt.Println("Encoded JSON:", string(encoded))
}