From 56e1bd6036006f4ce090e1048948106682cec696 Mon Sep 17 00:00:00 2001 From: zirain Date: Thu, 24 Oct 2024 14:23:41 +0800 Subject: [PATCH 1/2] chore: share UnitToDuration logic Signed-off-by: zirain --- internal/gatewayapi/backendtrafficpolicy.go | 21 ++---------- internal/utils/ratelimit/unit.go | 36 +++++++++++++++++++++ internal/xds/translator/local_ratelimit.go | 24 ++------------ 3 files changed, 42 insertions(+), 39 deletions(-) create mode 100644 internal/utils/ratelimit/unit.go diff --git a/internal/gatewayapi/backendtrafficpolicy.go b/internal/gatewayapi/backendtrafficpolicy.go index 3069ef5cc50..7a7cd1369b3 100644 --- a/internal/gatewayapi/backendtrafficpolicy.go +++ b/internal/gatewayapi/backendtrafficpolicy.go @@ -25,6 +25,7 @@ import ( "github.com/envoyproxy/gateway/internal/gatewayapi/status" "github.com/envoyproxy/gateway/internal/ir" "github.com/envoyproxy/gateway/internal/utils" + "github.com/envoyproxy/gateway/internal/utils/ratelimit" "github.com/envoyproxy/gateway/internal/utils/regex" ) @@ -642,9 +643,9 @@ func (t *Translator) buildLocalRateLimit(policy *egv1a1.BackendTrafficPolicy) (* // Validate that the rule limit unit is a multiple of the default limit unit. // This is required by Envoy local rateLimit implementation. // see https://github.com/envoyproxy/envoy/blob/6d9a6e995f472526de2b75233abca69aa00021ed/source/extensions/filters/common/local_ratelimit/local_ratelimit_impl.cc#L49 - defaultLimitUnit := ratelimitUnitToDuration(egv1a1.RateLimitUnit(defaultLimit.Unit)) + defaultLimitUnit := ratelimit.UnitToSeconds(egv1a1.RateLimitUnit(defaultLimit.Unit)) for _, rule := range local.Rules { - ruleLimitUint := ratelimitUnitToDuration(rule.Limit.Unit) + ruleLimitUint := ratelimit.UnitToSeconds(rule.Limit.Unit) if defaultLimitUnit == 0 || ruleLimitUint%defaultLimitUnit != 0 { return nil, fmt.Errorf("local rateLimit rule limit unit must be a multiple of the default limit unit") } @@ -788,22 +789,6 @@ func buildRateLimitRule(rule egv1a1.RateLimitRule) (*ir.RateLimitRule, error) { return irRule, nil } -func ratelimitUnitToDuration(unit egv1a1.RateLimitUnit) int64 { - var seconds int64 - - switch unit { - case egv1a1.RateLimitUnitSecond: - seconds = 1 - case egv1a1.RateLimitUnitMinute: - seconds = 60 - case egv1a1.RateLimitUnitHour: - seconds = 60 * 60 - case egv1a1.RateLimitUnitDay: - seconds = 60 * 60 * 24 - } - return seconds -} - func int64ToUint32(in int64) (uint32, bool) { if in >= 0 && in <= math.MaxUint32 { return uint32(in), true diff --git a/internal/utils/ratelimit/unit.go b/internal/utils/ratelimit/unit.go new file mode 100644 index 00000000000..a59ce475b21 --- /dev/null +++ b/internal/utils/ratelimit/unit.go @@ -0,0 +1,36 @@ +// Copyright Envoy Gateway Authors +// SPDX-License-Identifier: Apache-2.0 +// The full text of the Apache license is available in the LICENSE file at +// the root of the repo. + +package ratelimit + +import ( + "github.com/envoyproxy/gateway/internal/ir" + "google.golang.org/protobuf/types/known/durationpb" + + egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1" +) + +func UnitToSeconds(unit egv1a1.RateLimitUnit) int64 { + var seconds int64 + + switch unit { + case egv1a1.RateLimitUnitSecond: + seconds = 1 + case egv1a1.RateLimitUnitMinute: + seconds = 60 + case egv1a1.RateLimitUnitHour: + seconds = 60 * 60 + case egv1a1.RateLimitUnitDay: + seconds = 60 * 60 * 24 + } + return seconds +} + +func UnitToDuration(unit ir.RateLimitUnit) *durationpb.Duration { + seconds := UnitToSeconds(egv1a1.RateLimitUnit(unit)) + return &durationpb.Duration{ + Seconds: seconds, + } +} diff --git a/internal/xds/translator/local_ratelimit.go b/internal/xds/translator/local_ratelimit.go index ba330e22034..ea3c4351629 100644 --- a/internal/xds/translator/local_ratelimit.go +++ b/internal/xds/translator/local_ratelimit.go @@ -16,11 +16,11 @@ import ( hcmv3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3" typev3 "github.com/envoyproxy/go-control-plane/envoy/type/v3" "google.golang.org/protobuf/types/known/anypb" - "google.golang.org/protobuf/types/known/durationpb" "google.golang.org/protobuf/types/known/wrapperspb" egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1" "github.com/envoyproxy/gateway/internal/ir" + "github.com/envoyproxy/gateway/internal/utils/ratelimit" "github.com/envoyproxy/gateway/internal/xds/types" ) @@ -151,7 +151,7 @@ func (*localRateLimit) patchRoute(route *routev3.Route, irRoute *ir.HTTPRoute) e TokensPerFill: &wrapperspb.UInt32Value{ Value: uint32(local.Default.Requests), }, - FillInterval: ratelimitUnitToDuration(local.Default.Unit), + FillInterval: ratelimit.UnitToDuration(local.Default.Unit), }, FilterEnabled: &configv3.RuntimeFractionalPercent{ DefaultValue: &typev3.FractionalPercent{ @@ -281,7 +281,7 @@ func buildRouteLocalRateLimits(local *ir.LocalRateLimit) ( TokensPerFill: &wrapperspb.UInt32Value{ Value: uint32(rule.Limit.Requests), }, - FillInterval: ratelimitUnitToDuration(rule.Limit.Unit), + FillInterval: ratelimit.UnitToDuration(rule.Limit.Unit), }, } descriptors = append(descriptors, descriptor) @@ -289,21 +289,3 @@ func buildRouteLocalRateLimits(local *ir.LocalRateLimit) ( return rateLimits, descriptors, nil } - -func ratelimitUnitToDuration(unit ir.RateLimitUnit) *durationpb.Duration { - var seconds int64 - - switch egv1a1.RateLimitUnit(unit) { - case egv1a1.RateLimitUnitSecond: - seconds = 1 - case egv1a1.RateLimitUnitMinute: - seconds = 60 - case egv1a1.RateLimitUnitHour: - seconds = 60 * 60 - case egv1a1.RateLimitUnitDay: - seconds = 60 * 60 * 24 - } - return &durationpb.Duration{ - Seconds: seconds, - } -} From df7b07b988ce5f93b775b28c42684fdc7ed895cd Mon Sep 17 00:00:00 2001 From: zirain Date: Thu, 24 Oct 2024 14:48:22 +0800 Subject: [PATCH 2/2] lint Signed-off-by: zirain --- internal/utils/ratelimit/unit.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/utils/ratelimit/unit.go b/internal/utils/ratelimit/unit.go index a59ce475b21..94c8c7f6fbe 100644 --- a/internal/utils/ratelimit/unit.go +++ b/internal/utils/ratelimit/unit.go @@ -6,10 +6,10 @@ package ratelimit import ( - "github.com/envoyproxy/gateway/internal/ir" "google.golang.org/protobuf/types/known/durationpb" egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1" + "github.com/envoyproxy/gateway/internal/ir" ) func UnitToSeconds(unit egv1a1.RateLimitUnit) int64 {