Skip to content

Commit

Permalink
Increase API Client Rate Limit (#66)
Browse files Browse the repository at this point in the history
* bump rate limit to 3 qps

* add env var override for rate limiting
  • Loading branch information
smithclay authored Feb 2, 2022
1 parent 08fc85e commit 8711900
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .go-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.51.2
1.51.3
18 changes: 11 additions & 7 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/go-retryablehttp"
"github.com/lightstep/terraform-provider-lightstep/version"
"golang.org/x/time/rate"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"time"

"github.com/hashicorp/go-retryablehttp"
"github.com/lightstep/terraform-provider-lightstep/version"
"golang.org/x/time/rate"
)

const (
DefaultRateLimitPerSecond = 1
DefaultRateLimitPerSecond = 2
DefaultRetryMax = 3
DefaultUserAgent = "terraform-provider-lightstep"
)
Expand Down Expand Up @@ -94,7 +96,7 @@ func NewClientWithUserAgent(apiKey string, orgName string, env string, userAgent

// checkHTTPRetry inspects HTTP errors from the Lightstep API for known transient errors
func checkHTTPRetry(_ context.Context, resp *http.Response, err error) (bool, error) {
if resp.StatusCode == http.StatusInternalServerError {
if resp.StatusCode == http.StatusInternalServerError || resp.StatusCode == http.StatusServiceUnavailable {
return true, nil
}
return false, nil
Expand All @@ -120,8 +122,10 @@ func (c *Client) CallAPI(ctx context.Context, httpMethod string, suffix string,
}

func executeAPIRequest(ctx context.Context, c *Client, req *retryablehttp.Request, result interface{}) error {
if err := c.rateLimiter.Wait(ctx); err != nil {
return err
if len(os.Getenv("LS_DISABLE_RATE_LIMIT")) == 0 {
if err := c.rateLimiter.Wait(ctx); err != nil {
return err
}
}

resp, err := c.client.Do(req)
Expand Down

0 comments on commit 8711900

Please sign in to comment.