Skip to content

Commit

Permalink
limitCountRedis: limit quota headers MUST NOT occur multiple times
Browse files Browse the repository at this point in the history
Signed-off-by: spacewander <[email protected]>
  • Loading branch information
spacewander committed Feb 22, 2024
1 parent e7023ad commit 2074632
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
9 changes: 4 additions & 5 deletions plugins/limit_count_redis/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,12 @@ func (f *filter) EncodeHeaders(headers api.ResponseHeaderMap, endStream bool) ap
}

// According to the RFC, these headers MUST NOT occur multiple times.
headers.Add("x-ratelimit-limit", fmt.Sprintf("%d, %s", minCount, config.quotaPolicy))
headers.Set("x-ratelimit-limit", fmt.Sprintf("%d, %s", minCount, config.quotaPolicy))
if minRemain <= 0 {
headers.Add("x-ratelimit-remaining", "0")
headers.Set("x-ratelimit-remaining", "0")
} else {
headers.Add("x-ratelimit-remaining", strconv.FormatInt(minRemain, 10))
headers.Set("x-ratelimit-remaining", strconv.FormatInt(minRemain, 10))
}
headers.Add("x-ratelimit-remaining", strconv.FormatInt(minRemain, 10))
headers.Add("x-ratelimit-reset", strconv.FormatInt(minTTL, 10))
headers.Set("x-ratelimit-reset", strconv.FormatInt(minTTL, 10))
return api.Continue
}
12 changes: 6 additions & 6 deletions plugins/tests/integration/limit_count_redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ func TestLimitCountRedis(t *testing.T) {
hdr.Add("x-key", "1")
resp, _ := dp.Head("/echo", hdr)
assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, "1, 1;w=1", resp.Header.Get("X-Ratelimit-Limit"))
assert.Equal(t, "0", resp.Header.Get("X-Ratelimit-Remaining"))
assert.Equal(t, "1", resp.Header.Get("X-Ratelimit-Reset"))
assert.Equal(t, []string{"1, 1;w=1"}, resp.Header.Values("X-Ratelimit-Limit"))
assert.Equal(t, []string{"0"}, resp.Header.Values("X-Ratelimit-Remaining"))
assert.Equal(t, []string{"1"}, resp.Header.Values("X-Ratelimit-Reset"))
resp, _ = dp.Head("/echo", hdr)
assert.Equal(t, 429, resp.StatusCode)
assert.Equal(t, "1, 1;w=1", resp.Header.Get("X-Ratelimit-Limit"))
Expand Down Expand Up @@ -163,9 +163,9 @@ func TestLimitCountRedis(t *testing.T) {
hdr.Add("x-key", "1")
resp, _ := dp.Head("/echo", hdr)
assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, "2, 2;w=10, 2;w=1, 3;w=1", resp.Header.Get("X-Ratelimit-Limit"))
assert.Equal(t, "1", resp.Header.Get("X-Ratelimit-Remaining"))
assert.Equal(t, "10", resp.Header.Get("X-Ratelimit-Reset"))
assert.Equal(t, []string{"2, 2;w=10, 2;w=1, 3;w=1"}, resp.Header.Values("X-Ratelimit-Limit"))
assert.Equal(t, []string{"1"}, resp.Header.Values("X-Ratelimit-Remaining"))
assert.Equal(t, []string{"10"}, resp.Header.Values("X-Ratelimit-Reset"))

hdr2 := http.Header{}
hdr2.Add("x-key", "2")
Expand Down

0 comments on commit 2074632

Please sign in to comment.