Skip to content

Commit

Permalink
flag for rate limit is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyagara committed Dec 9, 2023
1 parent 35cfe0f commit daaefaf
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
6 changes: 4 additions & 2 deletions equinox.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/Kyagara/equinox/clients/tft"
"github.com/Kyagara/equinox/clients/val"
"github.com/Kyagara/equinox/internal"
"github.com/Kyagara/equinox/ratelimit"
"github.com/allegro/bigcache/v3"
"github.com/rs/zerolog"
)
Expand Down Expand Up @@ -77,8 +78,9 @@ func DefaultConfig(key string) (api.EquinoxConfig, error) {
HTTPClient: &http.Client{
Timeout: 15 * time.Second,
},
Retries: 3,
Cache: cache,
Retries: 3,
Cache: cache,
RateLimit: ratelimit.NewInternalRateLimit(),
}
return config, nil
}
1 change: 1 addition & 0 deletions equinox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func TestRateLimitWithMock(t *testing.T) {
}

config := util.NewTestEquinoxConfig()
config.RateLimit = ratelimit.NewInternalRateLimit()
config.LogLevel = zerolog.WarnLevel
config.Retries = 3

Expand Down
8 changes: 4 additions & 4 deletions internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func NewInternalClient(config api.EquinoxConfig) (*Client, error) {
mutex: sync.Mutex{},
},
cache: config.Cache,
ratelimit: ratelimit.NewInternalRateLimit(),
ratelimit: config.RateLimit,
maxRetries: config.Retries,
isCacheEnabled: config.Cache.TTL != 0,
isRateLimitEnabled: config.RateLimit.Enabled,
Expand Down Expand Up @@ -139,7 +139,7 @@ func (c *Client) Execute(ctx context.Context, equinoxReq *api.EquinoxRequest, ta
}
}

if !equinoxReq.IsCDN {
if c.isRateLimitEnabled && !equinoxReq.IsCDN {
err := c.ratelimit.Take(ctx, equinoxReq.Logger, equinoxReq.Route, equinoxReq.MethodID)
if err != nil {
return err
Expand Down Expand Up @@ -190,7 +190,7 @@ func (c *Client) Execute(ctx context.Context, equinoxReq *api.EquinoxRequest, ta
}

func (c *Client) checkResponse(equinoxReq *api.EquinoxRequest, response *http.Response) (time.Duration, error) {
if !equinoxReq.IsCDN {
if c.isRateLimitEnabled && !equinoxReq.IsCDN {
c.ratelimit.Update(equinoxReq.Logger, equinoxReq.Route, equinoxReq.MethodID, &response.Header)
}

Expand All @@ -201,7 +201,7 @@ func (c *Client) checkResponse(equinoxReq *api.EquinoxRequest, response *http.Re

// 4xx and 5xx responses will be retried
if equinoxReq.Retries < c.maxRetries {
if response.StatusCode == http.StatusTooManyRequests {
if response.StatusCode == http.StatusTooManyRequests && c.isRateLimitEnabled {
equinoxReq.Logger.Warn().Msg("Received 429 response, checking Retry-After header")
return c.ratelimit.CheckRetryAfter(equinoxReq.Route, equinoxReq.MethodID, &response.Header)
}
Expand Down
49 changes: 39 additions & 10 deletions internal/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,6 @@ func TestInternalClientErrorResponses(t *testing.T) {
wantErr: api.ErrUnsupportedMediaType,
code: 415,
},
{
name: "rate limited",
wantErr: api.ErrTooManyRequests,
code: 429,
},
{
name: "rate limited but no retry-after header found",
wantErr: ratelimit.Err429ButNoRetryAfterHeader,
code: 429,
},
{
name: "internal server error",
wantErr: api.ErrInternalServer,
Expand Down Expand Up @@ -224,6 +214,44 @@ func TestInternalClientErrorResponses(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
config := util.NewTestEquinoxConfig()

gock.New(fmt.Sprintf(api.RIOT_API_BASE_URL_FORMAT, "tests", "")).
Get("/").
Reply(test.code)

internal, err := internal.NewInternalClient(config)
require.NoError(t, err)
l := internal.Logger("client_endpoint_method")
ctx := context.Background()
equinoxReq, err := internal.Request(ctx, l, api.RIOT_API_BASE_URL_FORMAT, http.MethodGet, "tests", "/", "", nil)
require.NoError(t, err)
var gotData interface{}
gotErr := internal.Execute(ctx, equinoxReq, gotData)
require.Equal(t, test.wantErr, gotErr)
})
}

tests = []struct {
name string
wantErr error
code int
}{
{
name: "rate limited",
wantErr: api.ErrTooManyRequests,
code: 429,
},
{
name: "rate limited but no retry-after header found",
wantErr: ratelimit.Err429ButNoRetryAfterHeader,
code: 429,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
config := util.NewTestEquinoxConfig()
config.RateLimit = ratelimit.NewInternalRateLimit()
if test.name == "rate limited" {
config.Retries = 0
} else if test.name == "rate limited but no retry-after header found" {
Expand All @@ -250,6 +278,7 @@ func TestInternalClientErrorResponses(t *testing.T) {
func TestInternalClientRetries(t *testing.T) {
config := util.NewTestEquinoxConfig()
config.Retries = 3
config.RateLimit = ratelimit.NewInternalRateLimit()
internal, err := internal.NewInternalClient(config)
require.NoError(t, err)

Expand Down

0 comments on commit daaefaf

Please sign in to comment.