-
Notifications
You must be signed in to change notification settings - Fork 10
Add rate limit to Port client #262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
erikzaadi
wants to merge
6
commits into
main
Choose a base branch
from
PORT-15207-rate-limit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4817711
Add rate limit to Port client
erikzaadi 756a43a
[PR-FIX] Remove json info on RateInfo model
erikzaadi 8b31646
[PR-FIX] Mutex
erikzaadi 04ab603
[PR-FIX] De-cursorify
erikzaadi f234c5e
[DROP] Test for change
erikzaadi bd1e329
[SQUASH] Fix tests due to StupidErikException
erikzaadi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,156 @@ | ||
package cli | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func skipIfDisabled(t *testing.T) { | ||
if os.Getenv("PORT_RATE_LIMIT_DISABLED") != "" { | ||
t.Skip("Skipping rate limit test because PORT_RATE_LIMIT_DISABLED is set") | ||
} | ||
} | ||
|
||
func TestClientRateLimitIntegration(t *testing.T) { | ||
skipIfDisabled(t) | ||
|
||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("x-ratelimit-limit", "1000") | ||
w.Header().Set("x-ratelimit-period", "300") | ||
w.Header().Set("x-ratelimit-remaining", "50") | ||
w.Header().Set("x-ratelimit-reset", "120") | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write([]byte(`{"ok": true}`)) | ||
})) | ||
defer server.Close() | ||
|
||
client, err := New(server.URL) | ||
require.NoError(t, err) | ||
|
||
resp, err := client.Client.R().Get("/test") | ||
require.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, resp.StatusCode()) | ||
|
||
rateLimitInfo := client.GetRateLimitInfo() | ||
require.NotNil(t, rateLimitInfo) | ||
assert.Equal(t, 1000, rateLimitInfo.Limit) | ||
assert.Equal(t, 300, rateLimitInfo.Period) | ||
assert.Equal(t, 50, rateLimitInfo.Remaining) | ||
assert.Equal(t, 120, rateLimitInfo.Reset) | ||
} | ||
|
||
func TestClientRateLimitNoHeaders(t *testing.T) { | ||
skipIfDisabled(t) | ||
|
||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write([]byte(`{"ok": true}`)) | ||
})) | ||
defer server.Close() | ||
|
||
client, err := New(server.URL) | ||
require.NoError(t, err) | ||
|
||
resp, err := client.Client.R().Get("/test") | ||
require.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, resp.StatusCode()) | ||
|
||
rateLimitInfo := client.GetRateLimitInfo() | ||
assert.Nil(t, rateLimitInfo) | ||
} | ||
|
||
func TestClientRateLimitDisabled(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("x-ratelimit-limit", "1000") | ||
w.Header().Set("x-ratelimit-remaining", "1") | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write([]byte(`{"ok": true}`)) | ||
})) | ||
defer server.Close() | ||
|
||
client, err := New(server.URL, WithRateLimitDisabled()) | ||
require.NoError(t, err) | ||
|
||
resp, err := client.Client.R().Get("/test") | ||
require.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, resp.StatusCode()) | ||
|
||
rateLimitInfo := client.GetRateLimitInfo() | ||
assert.Nil(t, rateLimitInfo) | ||
} | ||
|
||
func TestClientRateLimitThrottling(t *testing.T) { | ||
skipIfDisabled(t) | ||
|
||
requestCount := 0 | ||
|
||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
requestCount++ | ||
w.Header().Set("x-ratelimit-limit", "100") | ||
w.Header().Set("x-ratelimit-remaining", "5") | ||
w.Header().Set("x-ratelimit-reset", "2") | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write([]byte(`{"ok": true}`)) | ||
})) | ||
defer server.Close() | ||
|
||
client, err := New(server.URL, WithRateLimitThreshold(0.1)) | ||
require.NoError(t, err) | ||
|
||
start := time.Now() | ||
resp, err := client.Client.R().Get("/test1") | ||
require.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, resp.StatusCode()) | ||
|
||
resp, err = client.Client.R().Get("/test2") | ||
elapsed := time.Since(start) | ||
|
||
require.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, resp.StatusCode()) | ||
assert.Equal(t, 2, requestCount) | ||
|
||
assert.Greater(t, elapsed, 10*time.Millisecond, "Request should have been throttled") | ||
} | ||
|
||
func TestClientRateLimitSettings(t *testing.T) { | ||
client, err := New("http://example.com") | ||
require.NoError(t, err) | ||
|
||
// somewhat dummy tests since we really can't test, so we check that they don't panic | ||
client.SetRateLimitEnabled(false) | ||
client.SetRateLimitEnabled(true) | ||
client.SetRateLimitThreshold(0.25) | ||
} | ||
|
||
func TestClientRateLimitDisabledViaEnv(t *testing.T) { | ||
t.Setenv("PORT_RATE_LIMIT_DISABLED", "1") | ||
|
||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("x-ratelimit-limit", "1000") | ||
w.Header().Set("x-ratelimit-remaining", "1") | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write([]byte(`{"ok": true}`)) | ||
})) | ||
defer server.Close() | ||
|
||
client, err := New(server.URL) | ||
require.NoError(t, err) | ||
|
||
start := time.Now() | ||
resp, err := client.Client.R().Get("/test") | ||
elapsed := time.Since(start) | ||
|
||
require.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, resp.StatusCode()) | ||
|
||
assert.Less(t, elapsed, 100*time.Millisecond, "Request should not be throttled when rate limiting is disabled") | ||
|
||
rateLimitInfo := client.GetRateLimitInfo() | ||
assert.Nil(t, rateLimitInfo) | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is too obscure. I think that you should return an error or panic if it is out of bounds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another option is to
clamp
the threshold between0
and1
.