From e4cd75d69c2279cd9463e1ac7b29390d7caf991f Mon Sep 17 00:00:00 2001 From: soltysss Date: Wed, 31 May 2023 19:55:30 +0300 Subject: [PATCH 1/2] SCALRCORE-26173 - Terraform provider > clients > handle rate limit --- README.md | 4 ++++ scalr.go | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/README.md b/README.md index 6a85d5c..a505bfc 100644 --- a/README.md +++ b/README.md @@ -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 ``` \ No newline at end of file diff --git a/scalr.go b/scalr.go index 2724133..48c6805 100644 --- a/scalr.go +++ b/scalr.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "io" + "log" "net/http" "net/url" "os" @@ -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 From df3ba0decf0bce739a2362c7718623ef05268643 Mon Sep 17 00:00:00 2001 From: soltysss Date: Tue, 13 Jun 2023 13:12:26 +0300 Subject: [PATCH 2/2] SCALRCORE-26173 - Terraform provider > clients > handle rate limit, fix tests --- scalr_test.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/scalr_test.go b/scalr_test.go index fee156e..02c0715 100644 --- a/scalr_test.go +++ b/scalr_test.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "net/http" "net/http/httptest" + "net/url" "os" "testing" ) @@ -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,