-
Notifications
You must be signed in to change notification settings - Fork 532
feat: Add per-unit response headers for all descriptors via LIMIT_ALL_DESCRIPTORS_HEADERS_ENABLED #1190
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
samay-arcana
wants to merge
2
commits into
envoyproxy:main
Choose a base branch
from
samay-arcana:samay/ratelimit-headers
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
feat: Add per-unit response headers for all descriptors via LIMIT_ALL_DESCRIPTORS_HEADERS_ENABLED #1190
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -1292,3 +1292,163 @@ func TestServiceMixedModeWithShadowMode(test *testing.T) { | |
| // Verify global shadow mode counter is incremented | ||
| t.assert.EqualValues(1, t.statStore.NewCounter("global_shadow_mode").Value()) | ||
| } | ||
|
|
||
| func TestServiceWithAllDescriptorsHeaders(test *testing.T) { | ||
| os.Setenv("LIMIT_ALL_DESCRIPTORS_HEADERS_ENABLED", "true") | ||
| defer func() { | ||
| os.Unsetenv("LIMIT_ALL_DESCRIPTORS_HEADERS_ENABLED") | ||
| }() | ||
|
|
||
| t := commonSetup(test) | ||
| defer t.controller.Finish() | ||
| service := t.setupBasicService() | ||
|
|
||
| // Config reload to pick up the env var. | ||
| barrier := newBarrier() | ||
| t.configUpdateEvent.EXPECT().GetConfig().DoAndReturn(func() (config.RateLimitConfig, any) { | ||
| barrier.signal() | ||
| return t.config, nil | ||
| }) | ||
| t.configUpdateEventChan <- t.configUpdateEvent | ||
| barrier.wait() | ||
|
|
||
| // Two descriptors with different time units (SECOND and MINUTE) | ||
| request := common.NewRateLimitRequest( | ||
| "test-domain", [][][2]string{{{"remote_address", "10.0.0.1"}}, {{"remote_address", "10.0.0.1"}}}, 1) | ||
| limits := []*config.RateLimit{ | ||
| config.NewRateLimit(10, pb.RateLimitResponse_RateLimit_SECOND, t.statsManager.NewStats("key1"), false, false, false, "", nil, false), | ||
| config.NewRateLimit(1000, pb.RateLimitResponse_RateLimit_MINUTE, t.statsManager.NewStats("key2"), false, false, false, "", nil, false), | ||
| } | ||
| t.config.EXPECT().GetLimit(context.Background(), "test-domain", request.Descriptors[0]).Return(limits[0]) | ||
| t.config.EXPECT().GetLimit(context.Background(), "test-domain", request.Descriptors[1]).Return(limits[1]) | ||
| t.cache.EXPECT().DoLimit(context.Background(), request, limits).Return( | ||
| []*pb.RateLimitResponse_DescriptorStatus{ | ||
| {Code: pb.RateLimitResponse_OK, CurrentLimit: limits[0].Limit, LimitRemaining: 9, DurationUntilReset: nil}, | ||
| {Code: pb.RateLimitResponse_OK, CurrentLimit: limits[1].Limit, LimitRemaining: 999, DurationUntilReset: nil}, | ||
| }) | ||
|
|
||
| response, err := service.ShouldRateLimit(context.Background(), request) | ||
| common.AssertProtoEqual( | ||
| t.assert, | ||
| &pb.RateLimitResponse{ | ||
| OverallCode: pb.RateLimitResponse_OK, | ||
| Statuses: []*pb.RateLimitResponse_DescriptorStatus{ | ||
| {Code: pb.RateLimitResponse_OK, CurrentLimit: limits[0].Limit, LimitRemaining: 9}, | ||
| {Code: pb.RateLimitResponse_OK, CurrentLimit: limits[1].Limit, LimitRemaining: 999}, | ||
| }, | ||
| ResponseHeadersToAdd: []*core.HeaderValue{ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The casing here is different than the title-case (RateLimit-Limit) emitted elsewhere |
||
| {Key: "ratelimit-limit-seconds", Value: "10"}, | ||
| {Key: "ratelimit-remaining-seconds", Value: "9"}, | ||
| {Key: "ratelimit-limit-minutes", Value: "1000"}, | ||
| {Key: "ratelimit-remaining-minutes", Value: "999"}, | ||
| }, | ||
| }, | ||
| response) | ||
| t.assert.Nil(err) | ||
| } | ||
|
|
||
| func TestServiceWithAllDescriptorsHeadersOverLimit(test *testing.T) { | ||
| os.Setenv("LIMIT_ALL_DESCRIPTORS_HEADERS_ENABLED", "true") | ||
| defer func() { | ||
| os.Unsetenv("LIMIT_ALL_DESCRIPTORS_HEADERS_ENABLED") | ||
| }() | ||
|
|
||
| t := commonSetup(test) | ||
| defer t.controller.Finish() | ||
| service := t.setupBasicService() | ||
|
|
||
| // Config reload to pick up the env var. | ||
| barrier := newBarrier() | ||
| t.configUpdateEvent.EXPECT().GetConfig().DoAndReturn(func() (config.RateLimitConfig, any) { | ||
| barrier.signal() | ||
| return t.config, nil | ||
| }) | ||
| t.configUpdateEventChan <- t.configUpdateEvent | ||
| barrier.wait() | ||
|
|
||
| // Two descriptors: SECOND is over limit, MINUTE is OK | ||
| request := common.NewRateLimitRequest( | ||
| "test-domain", [][][2]string{{{"remote_address", "10.0.0.1"}}, {{"remote_address", "10.0.0.1"}}}, 1) | ||
| limits := []*config.RateLimit{ | ||
| config.NewRateLimit(10, pb.RateLimitResponse_RateLimit_SECOND, t.statsManager.NewStats("key1"), false, false, false, "", nil, false), | ||
| config.NewRateLimit(1000, pb.RateLimitResponse_RateLimit_MINUTE, t.statsManager.NewStats("key2"), false, false, false, "", nil, false), | ||
| } | ||
| t.config.EXPECT().GetLimit(context.Background(), "test-domain", request.Descriptors[0]).Return(limits[0]) | ||
| t.config.EXPECT().GetLimit(context.Background(), "test-domain", request.Descriptors[1]).Return(limits[1]) | ||
| t.cache.EXPECT().DoLimit(context.Background(), request, limits).Return( | ||
| []*pb.RateLimitResponse_DescriptorStatus{ | ||
| {Code: pb.RateLimitResponse_OVER_LIMIT, CurrentLimit: limits[0].Limit, LimitRemaining: 0}, | ||
| {Code: pb.RateLimitResponse_OK, CurrentLimit: limits[1].Limit, LimitRemaining: 500}, | ||
| }) | ||
|
|
||
| response, err := service.ShouldRateLimit(context.Background(), request) | ||
| common.AssertProtoEqual( | ||
| t.assert, | ||
| &pb.RateLimitResponse{ | ||
| OverallCode: pb.RateLimitResponse_OVER_LIMIT, | ||
| Statuses: []*pb.RateLimitResponse_DescriptorStatus{ | ||
| {Code: pb.RateLimitResponse_OVER_LIMIT, CurrentLimit: limits[0].Limit, LimitRemaining: 0}, | ||
| {Code: pb.RateLimitResponse_OK, CurrentLimit: limits[1].Limit, LimitRemaining: 500}, | ||
| }, | ||
| ResponseHeadersToAdd: []*core.HeaderValue{ | ||
| {Key: "ratelimit-limit-seconds", Value: "10"}, | ||
| {Key: "ratelimit-remaining-seconds", Value: "0"}, | ||
| {Key: "ratelimit-limit-minutes", Value: "1000"}, | ||
| {Key: "ratelimit-remaining-minutes", Value: "500"}, | ||
| }, | ||
| }, | ||
| response) | ||
| t.assert.Nil(err) | ||
| } | ||
|
|
||
| func TestServiceWithAllDescriptorsHeadersSkipsNilLimits(test *testing.T) { | ||
| os.Setenv("LIMIT_ALL_DESCRIPTORS_HEADERS_ENABLED", "true") | ||
| defer func() { | ||
| os.Unsetenv("LIMIT_ALL_DESCRIPTORS_HEADERS_ENABLED") | ||
| }() | ||
|
|
||
| t := commonSetup(test) | ||
| defer t.controller.Finish() | ||
| service := t.setupBasicService() | ||
|
|
||
| // Config reload to pick up the env var. | ||
| barrier := newBarrier() | ||
| t.configUpdateEvent.EXPECT().GetConfig().DoAndReturn(func() (config.RateLimitConfig, any) { | ||
| barrier.signal() | ||
| return t.config, nil | ||
| }) | ||
| t.configUpdateEventChan <- t.configUpdateEvent | ||
| barrier.wait() | ||
|
|
||
| // One descriptor with limit, one without (nil limit) | ||
| request := common.NewRateLimitRequest( | ||
| "test-domain", [][][2]string{{{"remote_address", "10.0.0.1"}}, {{"other_key", "value"}}}, 1) | ||
| limits := []*config.RateLimit{ | ||
| config.NewRateLimit(10, pb.RateLimitResponse_RateLimit_SECOND, t.statsManager.NewStats("key1"), false, false, false, "", nil, false), | ||
| nil, | ||
| } | ||
| t.config.EXPECT().GetLimit(context.Background(), "test-domain", request.Descriptors[0]).Return(limits[0]) | ||
| t.config.EXPECT().GetLimit(context.Background(), "test-domain", request.Descriptors[1]).Return(limits[1]) | ||
| t.cache.EXPECT().DoLimit(context.Background(), request, limits).Return( | ||
| []*pb.RateLimitResponse_DescriptorStatus{ | ||
| {Code: pb.RateLimitResponse_OK, CurrentLimit: limits[0].Limit, LimitRemaining: 9}, | ||
| {Code: pb.RateLimitResponse_OK, CurrentLimit: nil, LimitRemaining: 0}, | ||
| }) | ||
|
|
||
| response, err := service.ShouldRateLimit(context.Background(), request) | ||
| common.AssertProtoEqual( | ||
| t.assert, | ||
| &pb.RateLimitResponse{ | ||
| OverallCode: pb.RateLimitResponse_OK, | ||
| Statuses: []*pb.RateLimitResponse_DescriptorStatus{ | ||
| {Code: pb.RateLimitResponse_OK, CurrentLimit: limits[0].Limit, LimitRemaining: 9}, | ||
| {Code: pb.RateLimitResponse_OK, CurrentLimit: nil, LimitRemaining: 0}, | ||
| }, | ||
| ResponseHeadersToAdd: []*core.HeaderValue{ | ||
| {Key: "ratelimit-limit-seconds", Value: "10"}, | ||
| {Key: "ratelimit-remaining-seconds", Value: "9"}, | ||
| }, | ||
| }, | ||
| response) | ||
| t.assert.Nil(err) | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
When both LIMIT_ALL_DESCRIPTORS_HEADERS_ENABLED and LIMIT_RESPONSE_HEADERS_ENABLED are enabled this block and line 273 will both be emitted twice. I think this block is redundant as lines 273-276 already does this. Maybe try a fourth test enabling both flags.