Skip to content

Commit

Permalink
chore: share UnitToDuration logic (#4510)
Browse files Browse the repository at this point in the history
* chore: share UnitToDuration logic

Signed-off-by: zirain <zirain2009@gmail.com>

* lint

Signed-off-by: zirain <zirain2009@gmail.com>

---------

Signed-off-by: zirain <zirain2009@gmail.com>
  • Loading branch information
zirain authored Oct 24, 2024
1 parent 0b69387 commit bc3df03
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
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
}
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,
}
}

0 comments on commit bc3df03

Please sign in to comment.