Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: share UnitToDuration logic #4510

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 3 additions & 18 deletions internal/gatewayapi/backendtrafficpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions internal/utils/ratelimit/unit.go
Original file line number Diff line number Diff line change
@@ -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 (
"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 {
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

Check warning on line 26 in internal/utils/ratelimit/unit.go

View check run for this annotation

Codecov / codecov/patch

internal/utils/ratelimit/unit.go#L25-L26

Added lines #L25 - L26 were not covered by tests
}
return seconds
}

func UnitToDuration(unit ir.RateLimitUnit) *durationpb.Duration {
seconds := UnitToSeconds(egv1a1.RateLimitUnit(unit))
return &durationpb.Duration{
Seconds: seconds,
}
}
24 changes: 3 additions & 21 deletions internal/xds/translator/local_ratelimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -281,29 +281,11 @@ 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)
}

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,
}
}