-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test for rate limit * Add HTTP retry * Retry test without body * Adding retry http client to configuration * lint fix --------- Co-authored-by: Peter Csajtai <[email protected]>
- Loading branch information
1 parent
a66708f
commit 34aa782
Showing
7 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type retryRoundTripper struct { | ||
http.RoundTripper | ||
maxRetries int | ||
} | ||
|
||
func Retry(transport http.RoundTripper, maxRetries int) http.RoundTripper { | ||
return &retryRoundTripper{RoundTripper: transport, maxRetries: maxRetries} | ||
} | ||
|
||
func (rt *retryRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) { | ||
bodyBuf, err := preReadRequestBodyAndClose(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var attempts int | ||
for { | ||
if bodyBuf != nil { | ||
r.Body = io.NopCloser(bodyBuf) | ||
} | ||
resp, err := rt.RoundTripper.RoundTrip(r) | ||
if err != nil { | ||
return resp, err | ||
} | ||
if resp.StatusCode != http.StatusTooManyRequests { | ||
return resp, nil | ||
} | ||
retryAfterHeader := resp.Header.Get("Retry-After") | ||
retryAfter, err := strconv.ParseInt(retryAfterHeader, 10, 64) | ||
if err != nil { | ||
return resp, err | ||
} | ||
|
||
if attempts >= rt.maxRetries { | ||
return resp, nil | ||
} | ||
|
||
if bodyBuf != nil { | ||
if _, err := bodyBuf.Seek(0, io.SeekStart); err != nil { | ||
return resp, err | ||
} | ||
} | ||
|
||
if resp.Body != nil { | ||
_, _ = io.Copy(io.Discard, resp.Body) | ||
_ = resp.Body.Close() | ||
} | ||
|
||
select { | ||
case <-time.After(time.Duration(retryAfter) * time.Second): | ||
attempts++ | ||
case <-r.Context().Done(): | ||
return nil, r.Context().Err() | ||
} | ||
} | ||
} | ||
|
||
func preReadRequestBodyAndClose(r *http.Request) (*bytes.Reader, error) { | ||
var reader *bytes.Reader | ||
if r.Body != nil && r.Body != http.NoBody { | ||
var buf bytes.Buffer | ||
if _, err := io.Copy(&buf, r.Body); err != nil { | ||
_ = r.Body.Close() | ||
return nil, err | ||
} | ||
_ = r.Body.Close() | ||
reader = bytes.NewReader(buf.Bytes()) | ||
} | ||
return reader, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package client | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestRetry_NoBody(t *testing.T) { | ||
attempts := 0 | ||
ts := httptest.NewServer( | ||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
attempts++ | ||
w.Header().Add("Retry-After", "1") | ||
w.WriteHeader(http.StatusTooManyRequests) | ||
})) | ||
defer ts.Close() | ||
|
||
client := &http.Client{ | ||
Transport: Retry(http.DefaultTransport, 2), | ||
} | ||
|
||
resp, err := client.Get(ts.URL) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusTooManyRequests, resp.StatusCode) | ||
assert.Equal(t, 3, attempts) | ||
} | ||
|
||
func TestRetry(t *testing.T) { | ||
attempts := 0 | ||
ts := httptest.NewServer( | ||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
attempts++ | ||
w.Header().Add("Retry-After", "1") | ||
w.WriteHeader(http.StatusTooManyRequests) | ||
})) | ||
defer ts.Close() | ||
|
||
client := &http.Client{ | ||
Transport: Retry(http.DefaultTransport, 2), | ||
} | ||
|
||
resp, err := client.Post(ts.URL, "text/plain", strings.NewReader("body")) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusTooManyRequests, resp.StatusCode) | ||
assert.Equal(t, 3, attempts) | ||
} | ||
|
||
func TestRetry_Eventually_Ok(t *testing.T) { | ||
attempts := 0 | ||
ts := httptest.NewServer( | ||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
attempts++ | ||
if attempts <= 2 { | ||
w.Header().Add("Retry-After", "1") | ||
w.WriteHeader(http.StatusTooManyRequests) | ||
} else { | ||
w.WriteHeader(http.StatusOK) | ||
} | ||
})) | ||
defer ts.Close() | ||
|
||
client := &http.Client{ | ||
Transport: Retry(http.DefaultTransport, 10), | ||
} | ||
|
||
resp, err := client.Post(ts.URL, "text/plain", strings.NewReader("body")) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
assert.Equal(t, 3, attempts) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
internal/configcat/testdata/TestAccLotsofConfigsDataSource/main.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
variable "product_id" { | ||
type = string | ||
} | ||
|
||
variable "name_filter_regex" { | ||
type = string | ||
default = null | ||
} | ||
|
||
variable "data_sources" { | ||
type = number | ||
} | ||
|
||
data "configcat_configs" "test" { | ||
count = var.data_sources | ||
|
||
product_id = var.product_id | ||
name_filter_regex = var.name_filter_regex | ||
} | ||
|
||
output "config_id" { | ||
value = length(data.configcat_configs.test[0].configs) > 0 ? data.configcat_configs.test[0].configs[0].config_id : null | ||
} |