Skip to content

Commit 04ccef1

Browse files
fix: use RateLimitBackoff client in LD API (#437)
#434 attempted to handle the rate limit from the LD API but the LD API client uses `http.DefaultClient` by default. This change passes in the client with `RateLimitBackoff` set into the call to init the LD API client.
1 parent fa34367 commit 04ccef1

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

internal/ld/ld.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func RateLimitBackoff(now func() time.Time, fallbackBackoff h.Backoff) func(time
8585
if s, ok := resp.Header["X-Ratelimit-Reset"]; ok {
8686
if sleepUntil, err := strconv.ParseInt(s[0], 10, 64); err == nil {
8787
sleep := sleepUntil - now().UnixMilli()
88+
log.Info.Printf("rate limit for %s %s hit, sleeping for %d ms", resp.Request.Method, resp.Request.URL, sleep)
8889

8990
if sleep > 0 {
9091
return time.Millisecond * time.Duration(sleep)
@@ -113,7 +114,8 @@ func InitApiClient(options ApiOptions) ApiClient {
113114

114115
return ApiClient{
115116
ldClient: ldapi.NewAPIClient(&ldapi.Configuration{
116-
UserAgent: options.UserAgent,
117+
HTTPClient: client.StandardClient(),
118+
UserAgent: options.UserAgent,
117119
Servers: []ldapi.ServerConfiguration{{
118120
URL: options.BaseUri,
119121
}},

internal/ld/ld_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ld
33
import (
44
"net/http"
55
"net/http/httptest"
6+
"net/url"
67
"os"
78
"testing"
89
"time"
@@ -260,9 +261,15 @@ func TestRateLimitBackoff(t *testing.T) {
260261
}
261262
for _, tt := range specs {
262263
t.Run(tt.name, func(t *testing.T) {
264+
testUrl, _ := url.Parse("http://test.example/")
265+
req := &http.Request{
266+
Method: "POST",
267+
URL: testUrl,
268+
}
263269
resp := &http.Response{
264270
StatusCode: tt.status,
265271
Header: make(http.Header),
272+
Request: req,
266273
}
267274

268275
if tt.rateLimitReset != nil {

0 commit comments

Comments
 (0)