feat: Add per-unit response headers for all descriptors via LIMIT_ALL_DESCRIPTORS_HEADERS_ENABLED#1190
Open
samay-arcana wants to merge 1 commit into
Open
Conversation
Signed-off-by: Samay Varshney <samay.varshney@arcana.io>
cc4cc7b to
26dab87
Compare
Author
|
@agrawroh can you please review this PR? |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
When
LIMIT_RESPONSE_HEADERS_ENABLEDis set, the ratelimit service currently returns response headers (RateLimit-Limit,RateLimit-Remaining,RateLimit-Reset) only for the single descriptor closest to hitting the rate limit. This means when multiple descriptors with different time units (e.g., SECOND and MINUTE) are evaluated, headers for only one unit are returned.This PR adds a new environment variable
LIMIT_ALL_DESCRIPTORS_HEADERS_ENABLEDthat, when enabled, returns per-unit limit and remaining headers for ALL descriptors that have configured limits.Example
Request with two descriptors (10 req/s and 1000 req/min):
{ "domain": "envoy", "descriptors": [ { "entries": [{ "key": "remote_address", "value": "10.0.0.1" }], "limit": { "requests_per_unit": 10, "unit": "SECOND" } }, { "entries": [{ "key": "remote_address", "value": "10.0.0.1" }], "limit": { "requests_per_unit": 1000, "unit": "MINUTE" } } ], "hits_addend": 1 }Before (with
LIMIT_RESPONSE_HEADERS_ENABLED=true):Only headers for the descriptor closest to limit are returned (e.g., only seconds).
After (with
LIMIT_ALL_DESCRIPTORS_HEADERS_ENABLED=true):{ "overall_code": "OK", "statuses": [...], "response_headers_to_add": [ { "key": "ratelimit-limit-seconds", "value": "10" }, { "key": "ratelimit-remaining-seconds", "value": "9" }, { "key": "ratelimit-limit-minutes", "value": "1000" }, { "key": "ratelimit-remaining-minutes", "value": "999" } ] }Changes
src/settings/settings.goRateLimitAllDescriptorsHeadersEnabledsetting (LIMIT_ALL_DESCRIPTORS_HEADERS_ENABLEDenv var, defaultfalse)src/service/ratelimit.goallDescriptorsHeadersEnabledfield to theservicestructSetConfig()to read the new setting on config reloadshouldRateLimitWorker(): whenallDescriptorsHeadersEnabledis true, generates per-unit headers for all descriptors instead of the single closest-to-limit descriptorunitToHeaderSuffix()helper: maps rate limit units to lowercase plural suffixes (SECOND→"seconds", MINUTE→"minutes", HOUR→"hours", etc.)allDescriptorsHeaders()method: iterates all descriptor statuses, skips those without limits, and generatesratelimit-limit-{unit}andratelimit-remaining-{unit}headerstest/service/ratelimit_test.goTestServiceWithAllDescriptorsHeaders: verifies SECOND + MINUTE descriptors produce 4 per-unit headersTestServiceWithAllDescriptorsHeadersOverLimit: verifies headers are returned even when OVER_LIMITTestServiceWithAllDescriptorsHeadersSkipsNilLimits: verifies descriptors without configured limits are skippedBackward Compatibility
LIMIT_ALL_DESCRIPTORS_HEADERS_ENABLEDdefaults tofalse— no behavior change unless explicitly enabledLIMIT_RESPONSE_HEADERS_ENABLEDbehavior is fully preservedLIMIT_ALL_DESCRIPTORS_HEADERS_ENABLEDtakes precedence overLIMIT_RESPONSE_HEADERS_ENABLEDwhen both are setSigned-off-by: Samay Varshney samay.varshney@arcana.io