From 8a9d392675f778629b8b9e1b3848fa512a68e8d0 Mon Sep 17 00:00:00 2001 From: Stefan Sedich Date: Tue, 3 Dec 2024 13:50:46 -0800 Subject: [PATCH] rate_limit: Add time unit week Signed-off-by: Stefan Sedich --- api/envoy/service/ratelimit/v3/rls.proto | 3 +++ changelogs/current.yaml | 3 +++ .../filters/http/ratelimit/ratelimit_headers.cc | 2 ++ .../filters/http/ratelimit/ratelimit_headers_test.cc | 11 +++++++++++ 4 files changed, 19 insertions(+) diff --git a/api/envoy/service/ratelimit/v3/rls.proto b/api/envoy/service/ratelimit/v3/rls.proto index d69a323d88b7a..63f2477a664e6 100644 --- a/api/envoy/service/ratelimit/v3/rls.proto +++ b/api/envoy/service/ratelimit/v3/rls.proto @@ -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; diff --git a/changelogs/current.yaml b/changelogs/current.yaml index f782b138a9374..748e451d695a5 100644 --- a/changelogs/current.yaml +++ b/changelogs/current.yaml @@ -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* diff --git a/source/extensions/filters/http/ratelimit/ratelimit_headers.cc b/source/extensions/filters/http/ratelimit/ratelimit_headers.cc index 12aec52a88a94..b91ac01f92ff6 100644 --- a/source/extensions/filters/http/ratelimit/ratelimit_headers.cc +++ b/source/extensions/filters/http/ratelimit/ratelimit_headers.cc @@ -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: diff --git a/test/extensions/filters/http/ratelimit/ratelimit_headers_test.cc b/test/extensions/filters/http/ratelimit/ratelimit_headers_test.cc index 9fba19f41da66..54894cfc8b13b 100644 --- a/test/extensions/filters/http/ratelimit/ratelimit_headers_test.cc +++ b/test/extensions/filters/http/ratelimit/ratelimit_headers_test.cc @@ -16,6 +16,7 @@ namespace RateLimitFilter { namespace { using Envoy::RateLimit::buildDescriptorStatus; +using Envoy::RateLimit::convertRateLimitUnit using Filters::Common::RateLimit::DescriptorStatusList; struct RateLimitHeadersTestCase { @@ -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