Skip to content

Commit

Permalink
Allow configuring go-retryablehttp.Logger (#626)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsomething authored Aug 14, 2023
1 parent 07db9dc commit f0c13f6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
9 changes: 7 additions & 2 deletions godo.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ type Client struct {
// Only the custom HTTP client's custom transport and timeout will be maintained.
type RetryConfig struct {
RetryMax int
RetryWaitMin *float64 // Minimum time to wait
RetryWaitMax *float64 // Maximum time to wait
RetryWaitMin *float64 // Minimum time to wait
RetryWaitMax *float64 // Maximum time to wait
Logger interface{} // Customer logger instance. Must implement either go-retryablehttp.Logger or go-retryablehttp.LeveledLogger
}

// RequestCompletionCallback defines the type of the request callback function
Expand Down Expand Up @@ -307,6 +308,9 @@ func New(httpClient *http.Client, opts ...ClientOpt) (*Client, error) {
retryableClient.RetryWaitMax = time.Duration(*c.RetryConfig.RetryWaitMax * float64(time.Second))
}

// By default this is nil and does not log.
retryableClient.Logger = c.RetryConfig.Logger

// if timeout is set, it is maintained before overwriting client with StandardClient()
retryableClient.HTTPClient.Timeout = c.HTTPClient.Timeout

Expand Down Expand Up @@ -373,6 +377,7 @@ func WithRetryAndBackoffs(retryConfig RetryConfig) ClientOpt {
c.RetryConfig.RetryMax = retryConfig.RetryMax
c.RetryConfig.RetryWaitMax = retryConfig.RetryWaitMax
c.RetryConfig.RetryWaitMin = retryConfig.RetryWaitMin
c.RetryConfig.Logger = retryConfig.Logger
return nil
}
}
Expand Down
49 changes: 49 additions & 0 deletions godo_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package godo

import (
"bytes"
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/http/httputil"
Expand Down Expand Up @@ -652,6 +654,53 @@ func TestWithRetryAndBackoffs(t *testing.T) {

}

func TestWithRetryAndBackoffsLogger(t *testing.T) {
// Mock server which always responds 500.
setup()
defer teardown()

url, _ := url.Parse(server.URL)
mux.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusAccepted)
})

tokenSrc := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: "new_token",
})

oauth_client := oauth2.NewClient(oauth2.NoContext, tokenSrc)

var buf bytes.Buffer
retryConfig := RetryConfig{
RetryMax: 3,
Logger: log.New(&buf, "", 0),
}

// Create the client. Use short retry windows so we fail faster.
client, err := New(oauth_client, WithRetryAndBackoffs(retryConfig))
client.BaseURL = url
if err != nil {
t.Fatalf("err: %v", err)
}

// Create the request
req, err := client.NewRequest(ctx, http.MethodGet, "/foo", nil)
if err != nil {
t.Fatalf("err: %v", err)
}

_, err = client.Do(context.Background(), req, nil)
if err != nil {
t.Fatalf("err: %v", err)
}

got := buf.String()
expected := fmt.Sprintf("[DEBUG] GET %s/foo\n", url)
if expected != got {
t.Fatalf("expected: %s; got: %s", expected, got)
}
}

func checkCurrentPage(t *testing.T, resp *Response, expectedPage int) {
links := resp.Links
p, err := links.CurrentPage()
Expand Down

0 comments on commit f0c13f6

Please sign in to comment.