Skip to content

Commit 5ded14a

Browse files
authored
CLOUDP-237245: Add example for retrying requests (#294)
1 parent b3496fc commit 5ded14a

File tree

11 files changed

+137
-14
lines changed

11 files changed

+137
-14
lines changed

.github/dependabot.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ updates:
55
schedule:
66
interval: weekly
77
day: monday
8+
- package-ecosystem: gomod
9+
directory: "/examples"
10+
schedule:
11+
interval: monthly
12+
ignore:
13+
- dependency-name: "*"
814
- package-ecosystem: github-actions
915
directory: "/"
1016
schedule:

.github/workflows/pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- name: lint
2121
uses: golangci/[email protected]
2222
with:
23-
version: v1.52.2
23+
version: v1.57.1
2424
args: --timeout=10m
2525

2626
tests-on-unix:

.golangci.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ linters-settings:
2020
- typeAssertChain
2121
gocyclo:
2222
min-complexity: 15
23-
govet:
24-
check-shadowing: true
25-
maligned:
26-
suggest-new: true
2723
revive:
2824
# see https://github.com/mgechev/revive#available-rules for details.
2925
ignore-generated-header: true
@@ -73,7 +69,7 @@ linters:
7369
disable-all: true
7470
enable:
7571
- bodyclose # checks whether HTTP response body is closed successfully [fast: false, auto-fix: false]
76-
- depguard # Go linter that checks if package imports are in a list of acceptable packages [fast: false, auto-fix: false]
72+
#- depguard # Go linter that checks if package imports are in a list of acceptable packages [fast: false, auto-fix: false]
7773
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f()) [fast: true, auto-fix: false]
7874
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases [fast: false, auto-fix: false]
7975
- errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13. [fast: false, auto-fix: false]

.tool-versions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
golang 1.20
1+
golang 1.22.1
22
nodejs 18.18.2
33

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
SOURCE_FILES?=./...
2-
GOLANGCI_VERSION=v1.52.2
2+
GOLANGCI_VERSION=v1.57.1
33
COVERAGE=coverage.out
44

55
export GO111MODULE := on

examples/README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Go SDK examples
1+
# Go SDK examples
22

33
## Running Examples
44

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

14-
1514
## Running Examples with Mocked Backend
1615

1716
SDK provides mocks using Testify and Mockery.
@@ -20,3 +19,13 @@ One of the SDK examples covers usage of the mockery within tests.
2019
```bash
2120
go test ./mock/cluster_test.go
2221
```
22+
23+
## Examples Reference
24+
25+
### Retry Example
26+
27+
Example provides automatic retries for all HTTP 500, 429 HTTP status errors.
28+
29+
```bash
30+
go run ./retry/retry.go
31+
```

examples/go.mod

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module go.mongodb.org/atlas-sdk/v20231115008/examples
2+
3+
go 1.22.1
4+
5+
replace go.mongodb.org/atlas-sdk/v20231115008 => ../
6+
7+
require (
8+
github.com/hashicorp/go-retryablehttp v0.7.5
9+
github.com/mongodb-forks/digest v1.1.0
10+
go.mongodb.org/atlas-sdk/v20231115008 v20231115008.1.0
11+
)
12+
13+
require (
14+
github.com/davecgh/go-spew v1.1.1 // indirect
15+
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
16+
github.com/pmezard/go-difflib v1.0.0 // indirect
17+
github.com/stretchr/objx v0.5.2 // indirect
18+
github.com/stretchr/testify v1.9.0
19+
gopkg.in/yaml.v3 v3.0.1 // indirect
20+
)

examples/go.sum

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/go-test/deep v1.1.0/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
4+
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
5+
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
6+
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
7+
github.com/hashicorp/go-retryablehttp v0.7.5 h1:bJj+Pj19UZMIweq/iie+1u5YCdGrnxCT9yvm0e+Nd5M=
8+
github.com/hashicorp/go-retryablehttp v0.7.5/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
9+
github.com/mongodb-forks/digest v1.1.0 h1:7eUdsR1BtqLv0mdNm4OXs6ddWvR4X2/OsLwdKksrOoc=
10+
github.com/mongodb-forks/digest v1.1.0/go.mod h1:rb+EX8zotClD5Dj4NdgxnJXG9nwrlx3NWKJ8xttz1Dg=
11+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
12+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
13+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
14+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
15+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
16+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
17+
go.mongodb.org/atlas-sdk/v20231115008 v20231115008.1.0 h1:WKPkreeCuIh2KKxfEP6KWIFNdNYaXnfSg79v2zAaSjw=
18+
go.mongodb.org/atlas-sdk/v20231115008 v20231115008.1.0/go.mod h1:BHskDmYvvANe+s/HMkRhvK3GudGEZuzjYSbp1fBssdc=
19+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
20+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
21+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

examples/retry/retry.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
"os"
8+
9+
"context"
10+
11+
"go.mongodb.org/atlas-sdk/v20231115008/admin"
12+
13+
retryablehttp "github.com/hashicorp/go-retryablehttp"
14+
"github.com/mongodb-forks/digest"
15+
)
16+
17+
/*
18+
* MongoDB Atlas Go SDK Retryable Request Example
19+
*
20+
* Example using custom http client that handles rate limiting and 500 Http errors by retrying requests automatically.
21+
* Example uses https://pkg.go.dev/github.com/hashicorp/go-retryablehttp.
22+
* Please refer to the package documentation for more information.
23+
*/
24+
func main() {
25+
ctx := context.Background()
26+
// Values provided as part of env variables
27+
// See: https://www.mongodb.com/docs/atlas/app-services/authentication/api-key/
28+
apiKey := os.Getenv("MONGODB_ATLAS_PUBLIC_KEY")
29+
apiSecret := os.Getenv("MONGODB_ATLAS_PRIVATE_KEY")
30+
url := os.Getenv("MONGODB_ATLAS_URL")
31+
32+
// Using custom client
33+
// This example relies on https://pkg.go.dev/github.com/hashicorp/go-retryablehttp
34+
// retryablehttp performs automatic retries under certain conditions.
35+
// Mainly, if an error is returned by the client (connection errors etc),
36+
/// or if a 500-range response is received, then a retry is invoked.
37+
retryClient := retryablehttp.NewClient()
38+
retryClient.RetryMax = 3
39+
40+
retryableClient, err := newRetryableClient(retryClient, apiKey, apiSecret)
41+
if err != nil {
42+
log.Fatal("Cannot instantiate client")
43+
}
44+
sdk, err := admin.NewClient(
45+
admin.UseHTTPClient(retryableClient),
46+
admin.UseBaseURL(url),
47+
admin.UseDebug(false))
48+
if err != nil {
49+
log.Fatal(err)
50+
}
51+
52+
request := sdk.ProjectsApi.ListProjectsWithParams(ctx,
53+
&admin.ListProjectsApiParams{
54+
ItemsPerPage: admin.PtrInt(1),
55+
IncludeCount: admin.PtrBool(true),
56+
PageNum: admin.PtrInt(1),
57+
})
58+
projects, _, err := request.Execute()
59+
if err != nil {
60+
log.Fatal(err)
61+
}
62+
63+
fmt.Println("Total Projects", projects.GetTotalCount())
64+
65+
}
66+
67+
func newRetryableClient(retryClient *retryablehttp.Client, apiKey string, apiSecret string) (*http.Client, error) {
68+
var transport http.RoundTripper = &retryablehttp.RoundTripper{Client: retryClient}
69+
digestRetryAbleTransport := digest.NewTransportWithHTTPRoundTripper(apiKey, apiSecret, transport)
70+
return digestRetryAbleTransport.Client()
71+
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module go.mongodb.org/atlas-sdk/v20231115008
22

3-
go 1.20
3+
go 1.22.1
44

55
require (
66
github.com/go-test/deep v1.1.0
7-
github.com/mongodb-forks/digest v1.0.5
7+
github.com/mongodb-forks/digest v1.1.0
88
)
99

1010
require (

0 commit comments

Comments
 (0)