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

added tries and incremental delay #321

Merged
merged 3 commits into from
Oct 6, 2022
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
13 changes: 10 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
package client

import (
"net/http"
"time"

"github.com/go-resty/resty/v2"
"github.com/rs/zerolog/log"
"net/http"
)

// DefaultBaseURL is the default base URL for the Rollbar API.
Expand All @@ -46,8 +48,13 @@ func NewClient(baseURL, token string) *RollbarAPIClient {
r := resty.New()

// Use default transport - needed for VCR
r.SetTransport(http.DefaultTransport)

r.SetTransport(http.DefaultTransport).
// set timeout on http client
SetTimeout(30 * time.Second).
// Set retry count to 4 (try 5 times before it fails)
SetRetryCount(4).
SetRetryWaitTime(8 * time.Second).
SetRetryMaxWaitTime(50 * time.Second)
// Authentication
if token != "" {
r = r.SetHeaders(map[string]string{
Expand Down
53 changes: 48 additions & 5 deletions client/client_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@
package client

import (
"io/ioutil"
"net/http"
"os"
"testing"
"time"

"github.com/brianvoe/gofakeit/v5"
"github.com/go-resty/resty/v2"
"github.com/jarcoal/httpmock"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/suite"
"io/ioutil"
"net/http"
"os"
"testing"
)

func loadFixture(fixturePath string) string {
Expand Down Expand Up @@ -75,6 +78,45 @@ type Suite struct {
client *RollbarAPIClient
}

func NewTestClient(baseURL, token string) *RollbarAPIClient {
log.Debug().Msg("Initializing Rollbar client")

// New Resty HTTP client
r := resty.New()

// Use default transport - needed for VCR
r.SetTransport(http.DefaultTransport).
// set timeout on http client
SetTimeout(30 * time.Second).
// Set retry count to 3 (try 3 times before it fails)
SetRetryCount(2).
SetRetryWaitTime(1 * time.Second).
SetRetryMaxWaitTime(5 * time.Second)

// Authentication
if token != "" {
r = r.SetHeaders(map[string]string{
"X-Rollbar-Access-Token": token,
"X-Rollbar-Terraform": "true"})
} else {
log.Warn().Msg("Rollbar API token not set")
}

// Authentication
if baseURL == "" {
log.Error().Msg("Rollbar API base URL not set")
}

// Configure Resty to use Zerolog for logging
r.SetLogger(restyZeroLogger{log.Logger})

// Rollbar client
c := RollbarAPIClient{
Resty: r,
BaseURL: baseURL,
}
return &c
}
func (s *Suite) SetupSuite() {
// Pretty logging
log.Logger = log.
Expand All @@ -87,7 +129,8 @@ func (s *Suite) SetupSuite() {
gofakeit.Seed(0) // Setting seed to 0 will use time.Now().UnixNano()

// Setup RollbarAPIClient and enable mocking
c := NewClient(DefaultBaseURL, "fakeTokenString")
c := NewTestClient(DefaultBaseURL, "fakeTokenString")

httpmock.ActivateNonDefault(c.Resty.GetClient())
s.client = c
}
Expand Down