Skip to content

Commit

Permalink
rate_limit: Add time unit week
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Sedich <[email protected]>
  • Loading branch information
stefansedich committed Dec 4, 2024
1 parent 8d1086d commit 8a9d392
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
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 @@ -85,6 +85,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
11 changes: 11 additions & 0 deletions test/extensions/filters/http/ratelimit/ratelimit_headers_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace RateLimitFilter {
namespace {

using Envoy::RateLimit::buildDescriptorStatus;
using Envoy::RateLimit::convertRateLimitUnit
using Filters::Common::RateLimit::DescriptorStatusList;

struct RateLimitHeadersTestCase {
Expand Down Expand Up @@ -83,6 +84,16 @@ TEST_P(RateLimitHeadersTest, RateLimitHeadersTest) {
EXPECT_THAT(result, HeaderMapEqual(&GetParam().expected_headers));
}

TEST(RateLimitHeadersTest, ConvertRateLimitUnitTest) {
EXPECT_EQ(1, convertRateLimitUnit(envoy::service::ratelimit::v3::RateLimitResponse::RateLimit::SECOND));
EXPECT_EQ(60, convertRateLimitUnit(envoy::service::ratelimit::v3::RateLimitResponse::RateLimit::MINUTE));
EXPECT_EQ(60 * 60, convertRateLimitUnit(envoy::service::ratelimit::v3::RateLimitResponse::RateLimit::HOUR));
EXPECT_EQ(24 * 60 * 60, convertRateLimitUnit(envoy::service::ratelimit::v3::RateLimitResponse::RateLimit::DAY));
EXPECT_EQ(7 * 24 * 60 * 60, convertRateLimitUnit(envoy::service::ratelimit::v3::RateLimitResponse::RateLimit::WEEK));
EXPECT_EQ(30 * 24 * 60 * 60, convertRateLimitUnit(envoy::service::ratelimit::v3::RateLimitResponse::RateLimit::MONTH));
EXPECT_EQ(365 * 24 * 60 * 60, convertRateLimitUnit(envoy::service::ratelimit::v3::RateLimitResponse::RateLimit::YEAR));
}

} // namespace
} // namespace RateLimitFilter
} // namespace HttpFilters
Expand Down

0 comments on commit 8a9d392

Please sign in to comment.