Skip to content

Commit

Permalink
Merged automatically by CI pipeline
Browse files Browse the repository at this point in the history
SCALRCORE-26173 - Terraform provider > clients > handle rate limit
  • Loading branch information
emocharnik authored Jun 13, 2023
2 parents f8be8d1 + df3ba0d commit 4ea986e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,8 @@ export SCALR_TOKEN=
You can run the acceptance tests like this:
```
make test
```
To run specific test:
```
TESTARGS="-run TestAccessPoliciesList/without_list_options" make test
```
7 changes: 7 additions & 0 deletions scalr.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -265,6 +266,12 @@ func (c *Client) retryHTTPCheck(ctx context.Context, resp *http.Response, err er
return c.retryServerErrors, err
}
if resp.StatusCode == 429 || (c.retryServerErrors && resp.StatusCode >= 500) {
if resp.StatusCode == 429 {
log.Printf(
"[DEBUG] API rate limit reached for %s%s, retrying...",
resp.Request.URL.Host, resp.Request.URL.Path,
)
}
return true, nil
}
return false, nil
Expand Down
21 changes: 17 additions & 4 deletions scalr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
)
Expand Down Expand Up @@ -190,26 +191,38 @@ func TestClient_retryHTTPCheck(t *testing.T) {
checkErr error
}{
"429-no-server-errors": {
resp: &http.Response{StatusCode: 429},
resp: &http.Response{
StatusCode: 429,
Request: &http.Request{URL: &url.URL{Host: "scalr.test", Path: "/test/thing"}},
},
err: nil,
checkOK: true,
checkErr: nil,
},
"429-with-server-errors": {
resp: &http.Response{StatusCode: 429},
resp: &http.Response{
StatusCode: 429,
Request: &http.Request{URL: &url.URL{Host: "scalr.test", Path: "/test/thing"}},
},
err: nil,
retryServerErrors: true,
checkOK: true,
checkErr: nil,
},
"500-no-server-errors": {
resp: &http.Response{StatusCode: 500},
resp: &http.Response{
StatusCode: 500,
Request: &http.Request{URL: &url.URL{Host: "scalr.test", Path: "/test/thing"}},
},
err: nil,
checkOK: false,
checkErr: nil,
},
"500-with-server-errors": {
resp: &http.Response{StatusCode: 500},
resp: &http.Response{
StatusCode: 500,
Request: &http.Request{URL: &url.URL{Host: "scalr.test", Path: "/test/thing"}},
},
err: nil,
retryServerErrors: true,
checkOK: true,
Expand Down

0 comments on commit 4ea986e

Please sign in to comment.