Skip to content

Commit

Permalink
rate_limit: Add time unit week (#37494)
Browse files Browse the repository at this point in the history
Following on from #24070 and
adding support for week as a unit of time for ratelimits as this is
something my team currently needs and WEEK felt sad as it was missing
the protobuf party.

Ideally we would have something like
#33277 but in the interim it
makes sense to add WEEK here since we have every other period already
except week.

Signed-off-by: Stefan Sedich <[email protected]>
  • Loading branch information
stefansedich authored Dec 9, 2024
1 parent 3a89d3e commit 33fb499
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
3 changes: 3 additions & 0 deletions api/envoy/service/ratelimit/v3/rls.proto
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ message RateLimitResponse {
// The time unit representing a day.
DAY = 4;

// The time unit representing a week.
WEEK = 7;

// The time unit representing a month.
MONTH = 5;

Expand Down
3 changes: 3 additions & 0 deletions changelogs/current.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ minor_behavior_changes:
change: |
Increase only the statistics counter ``missing_source_origin`` for requests with a missing source origin.
Previously, the ``request_invalid`` counter was also increased for such requests.
- area: rate_limit
change: |
add ``WEEK`` to the unit of time for rate limit.
bug_fixes:
# *Changes expected to improve the state of the world and are unlikely to have negative effects*
Expand Down
2 changes: 2 additions & 0 deletions source/extensions/filters/http/ratelimit/ratelimit_headers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ uint32_t XRateLimitHeaderUtils::convertRateLimitUnit(
return 60 * 60;
case envoy::service::ratelimit::v3::RateLimitResponse::RateLimit::DAY:
return 24 * 60 * 60;
case envoy::service::ratelimit::v3::RateLimitResponse::RateLimit::WEEK:
return 7 * 24 * 60 * 60;
case envoy::service::ratelimit::v3::RateLimitResponse::RateLimit::MONTH:
return 30 * 24 * 60 * 60;
case envoy::service::ratelimit::v3::RateLimitResponse::RateLimit::YEAR:
Expand Down
41 changes: 40 additions & 1 deletion test/extensions/filters/http/ratelimit/ratelimit_headers_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,46 @@ class RateLimitHeadersTest : public testing::TestWithParam<RateLimitHeadersTestC
"first", 2, 3),
buildDescriptorStatus(4,
envoy::service::ratelimit::v3::RateLimitResponse::RateLimit::HOUR,
"second", 5, 6)}}, );
"second", 5, 6)}},
// Test unit conversions
{{{"x-ratelimit-limit", "1, 1;w=1;name=\"unit\""},
{"x-ratelimit-remaining", "1"},
{"x-ratelimit-reset", "1"}},
{buildDescriptorStatus(1,
envoy::service::ratelimit::v3::RateLimitResponse::RateLimit::SECOND,
"unit", 1, 1)}},
{{{"x-ratelimit-limit", "1, 1;w=60;name=\"unit\""},
{"x-ratelimit-remaining", "1"},
{"x-ratelimit-reset", "1"}},
{buildDescriptorStatus(1,
envoy::service::ratelimit::v3::RateLimitResponse::RateLimit::MINUTE,
"unit", 1, 1)}},
{{{"x-ratelimit-limit", "1, 1;w=3600;name=\"unit\""},
{"x-ratelimit-remaining", "1"},
{"x-ratelimit-reset", "1"}},
{buildDescriptorStatus(
1, envoy::service::ratelimit::v3::RateLimitResponse::RateLimit::HOUR, "unit", 1, 1)}},
{{{"x-ratelimit-limit", "1, 1;w=86400;name=\"unit\""},
{"x-ratelimit-remaining", "1"},
{"x-ratelimit-reset", "1"}},
{buildDescriptorStatus(1, envoy::service::ratelimit::v3::RateLimitResponse::RateLimit::DAY,
"unit", 1, 1)}},
{{{"x-ratelimit-limit", "1, 1;w=604800;name=\"unit\""},
{"x-ratelimit-remaining", "1"},
{"x-ratelimit-reset", "1"}},
{buildDescriptorStatus(
1, envoy::service::ratelimit::v3::RateLimitResponse::RateLimit::WEEK, "unit", 1, 1)}},
{{{"x-ratelimit-limit", "1, 1;w=2592000;name=\"unit\""},
{"x-ratelimit-remaining", "1"},
{"x-ratelimit-reset", "1"}},
{buildDescriptorStatus(
1, envoy::service::ratelimit::v3::RateLimitResponse::RateLimit::MONTH, "unit", 1, 1)}},
{{{"x-ratelimit-limit", "1, 1;w=31536000;name=\"unit\""},
{"x-ratelimit-remaining", "1"},
{"x-ratelimit-reset", "1"}},
{buildDescriptorStatus(1,
envoy::service::ratelimit::v3::RateLimitResponse::RateLimit::YEAR,
"unit", 1, 1)}}, );
}
};

Expand Down

0 comments on commit 33fb499

Please sign in to comment.