feat: Add per-unit response headers for all descriptors via LIMIT_ALL_DESCRIPTORS_HEADERS_ENABLED - #1190
feat: Add per-unit response headers for all descriptors via LIMIT_ALL_DESCRIPTORS_HEADERS_ENABLED#1190samay-arcana wants to merge 2 commits into
Conversation
Signed-off-by: Samay Varshney <samay.varshney@arcana.io>
cc4cc7b to
26dab87
Compare
|
@agrawroh can you please review this PR? |
|
I'm really interested into this, I was just about start working on a PR but find this one 🚀 |
|
Hey @samay-arcana LGTM, but let's try to ping some maintainer as I'm not, and not really aware of this codebase. |
|
@collin-lee pls review |
|
Should we also update README.md to mention this feature/changes? |
| if this.allDescriptorsHeadersEnabled { | ||
| response.ResponseHeadersToAdd = this.allDescriptorsHeaders(responseDescriptorStatuses) | ||
| // Also include the standard min-descriptor headers for backwards compatibility | ||
| if this.customHeadersEnabled && minimumDescriptor != nil { |
There was a problem hiding this comment.
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.
| {Code: pb.RateLimitResponse_OK, CurrentLimit: limits[0].Limit, LimitRemaining: 9}, | ||
| {Code: pb.RateLimitResponse_OK, CurrentLimit: limits[1].Limit, LimitRemaining: 999}, | ||
| }, | ||
| ResponseHeadersToAdd: []*core.HeaderValue{ |
There was a problem hiding this comment.
The casing here is different than the title-case (RateLimit-Limit) emitted elsewhere
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