Skip to content

Commit

Permalink
add options
Browse files Browse the repository at this point in the history
  • Loading branch information
taratorio committed Nov 8, 2024
1 parent 9b3c1c5 commit ff32bea
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
33 changes: 26 additions & 7 deletions polygon/heimdall/client_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,41 @@ type HttpRequest struct {
start time.Time
}

func NewHttpClient(urlString string, logger log.Logger) *HttpClient {
httpClient := &http.Client{
Timeout: apiHeimdallTimeout,
type HttpClientOption func(*HttpClient)

func WithHttpRequestHandler(handler httpRequestHandler) HttpClientOption {
return func(client *HttpClient) {
client.handler = handler
}
}

func WithHttpRetryBackOff(retryBackOff time.Duration) HttpClientOption {
return func(client *HttpClient) {
client.retryBackOff = retryBackOff
}
}

func WithHttpMaxRetries(maxRetries int) HttpClientOption {
return func(client *HttpClient) {
client.maxRetries = maxRetries
}
return newHttpClient(urlString, httpClient, retryBackOff, maxRetries, logger)
}

func newHttpClient(urlString string, handler httpRequestHandler, retryBackOff time.Duration, maxRetries int, logger log.Logger) *HttpClient {
return &HttpClient{
func NewHttpClient(urlString string, logger log.Logger, opts ...HttpClientOption) *HttpClient {
c := &HttpClient{
urlString: urlString,
logger: logger,
handler: handler,
handler: &http.Client{Timeout: apiHeimdallTimeout},
retryBackOff: retryBackOff,
maxRetries: maxRetries,
closeCh: make(chan struct{}),
}

for _, opt := range opts {
opt(c)
}

return c
}

const (
Expand Down
16 changes: 14 additions & 2 deletions polygon/heimdall/client_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ func TestHeimdallClientFetchesTerminateUponTooManyErrors(t *testing.T) {
}, nil).
Times(5)
logger := testlog.Logger(t, log.LvlDebug)
heimdallClient := newHttpClient("https://dummyheimdal.com", requestHandler, 100*time.Millisecond, 5, logger)
heimdallClient := NewHttpClient(
"https://dummyheimdal.com",
logger,
WithHttpRequestHandler(requestHandler),
WithHttpRetryBackOff(100*time.Millisecond),
WithHttpMaxRetries(5),
)

spanRes, err := heimdallClient.FetchSpan(ctx, 1534)
require.Nil(t, spanRes)
Expand All @@ -72,7 +78,13 @@ func TestHeimdallClientStateSyncEventsReturnsErrNoResponseWhenHttp200WithEmptyBo
}, nil).
Times(2)
logger := testlog.Logger(t, log.LvlDebug)
heimdallClient := newHttpClient("https://dummyheimdal.com", requestHandler, time.Millisecond, 2, logger)
heimdallClient := NewHttpClient(
"https://dummyheimdal.com",
logger,
WithHttpRequestHandler(requestHandler),
WithHttpRetryBackOff(time.Millisecond),
WithHttpMaxRetries(2),
)

spanRes, err := heimdallClient.FetchStateSyncEvents(ctx, 100, time.Now(), 0)
require.Nil(t, spanRes)
Expand Down

0 comments on commit ff32bea

Please sign in to comment.