Skip to content

Commit

Permalink
Merge pull request #34 from whalecold/chore/limit
Browse files Browse the repository at this point in the history
fix:  use the correct envoy field to set the limit policy
  • Loading branch information
whalecold authored Sep 4, 2024
2 parents 4e15c4b + 7cf1c73 commit 5c8bf68
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ spec:
token_bucket:
# the qps limit
max_tokens: 4
tokens_per_fill: 10
workloadSelector:
labels:
# the label of the service pod.
Expand Down
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ spec:
token_bucket:
# 限流参数
max_tokens: 4
tokens_per_fill: 10
workloadSelector:
labels:
# 服务实例 pod 的标签,根据实际情况填写
Expand Down
6 changes: 4 additions & 2 deletions core/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,15 @@ func TestRegisterXDSUpdateHandler(t *testing.T) {
{
RoutePort: 80,
InlineRouteConfig: &xdsresource.RouteConfigResource{
MaxTokens: 100,
MaxTokens: 100,
TokensPerFill: 100,
},
},
{
RoutePort: 0,
InlineRouteConfig: &xdsresource.RouteConfigResource{
MaxTokens: 1000,
MaxTokens: 1000,
TokensPerFill: 100,
},
},
},
Expand Down
24 changes: 15 additions & 9 deletions core/xdsresource/lds.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func unmarshallHTTPConnectionManager(rawResources *any.Any) (string, *RouteConfi
if err := proto.Unmarshal(rawResources.GetValue(), httpConnMng); err != nil {
return "", nil, fmt.Errorf("unmarshal HttpConnectionManager failed: %s", err)
}
maxTokens, err := getLocalRateLimitFromHttpConnectionManager(httpConnMng)
maxTokens, tokensPerfill, err := getLocalRateLimitFromHttpConnectionManager(httpConnMng)
if err != nil {
return "", nil, err
}
Expand All @@ -223,7 +223,8 @@ func unmarshallHTTPConnectionManager(rawResources *any.Any) (string, *RouteConfi
return "", nil, fmt.Errorf("no route config Name")
}
return httpConnMng.GetRds().GetRouteConfigName(), &RouteConfigResource{
MaxTokens: maxTokens,
MaxTokens: maxTokens,
TokensPerFill: tokensPerfill,
}, nil
case *v3httppb.HttpConnectionManager_RouteConfig:
var rcfg *v3routepb.RouteConfiguration
Expand All @@ -235,12 +236,13 @@ func unmarshallHTTPConnectionManager(rawResources *any.Any) (string, *RouteConfi
return "", nil, err
}
inlineRouteConfig.MaxTokens = maxTokens
inlineRouteConfig.TokensPerFill = tokensPerfill
return httpConnMng.GetRouteConfig().GetName(), inlineRouteConfig, nil
}
return "", nil, nil
}

func getLocalRateLimitFromHttpConnectionManager(hcm *v3httppb.HttpConnectionManager) (uint32, error) {
func getLocalRateLimitFromHttpConnectionManager(hcm *v3httppb.HttpConnectionManager) (uint32, uint32, error) {
for _, filter := range hcm.HttpFilters {
switch filter.ConfigType.(type) {
case *v3httppb.HttpFilter_TypedConfig:
Expand All @@ -252,16 +254,16 @@ func getLocalRateLimitFromHttpConnectionManager(hcm *v3httppb.HttpConnectionMana
case RateLimitTypeURL:
lrl := &ratelimitv3.LocalRateLimit{}
if err := proto.Unmarshal(typedConfig, lrl); err != nil {
return 0, fmt.Errorf("unmarshal LocalRateLimit failed: %s", err)
return 0, 0, fmt.Errorf("unmarshal LocalRateLimit failed: %s", err)
}
if lrl.TokenBucket != nil {
return lrl.TokenBucket.MaxTokens, nil
return lrl.TokenBucket.MaxTokens, lrl.TokenBucket.TokensPerFill.GetValue(), nil
}
case TypedStructTypeURL:
// ratelimit may be configured with udpa struct.
ts := &udpatypev1.TypedStruct{}
if err := proto.Unmarshal(typedConfig, ts); err != nil {
return 0, fmt.Errorf("unmarshal TypedStruct failed: %s", err)
return 0, 0, fmt.Errorf("unmarshal TypedStruct failed: %s", err)
}
tokenBucket, ok := ts.GetValue().GetFields()["token_bucket"]
if !ok {
Expand All @@ -271,10 +273,14 @@ func getLocalRateLimitFromHttpConnectionManager(hcm *v3httppb.HttpConnectionMana
if !ok {
continue
}
return uint32(maxTokens.GetNumberValue()), nil
tokensPerfill, ok := tokenBucket.GetStructValue().GetFields()["tokens_per_fill"]
if !ok {
continue
}
return uint32(maxTokens.GetNumberValue()), uint32(tokensPerfill.GetNumberValue()), nil
}
}
return 0, nil
return 0, 0, nil
}
return 0, nil
return 0, 0, nil
}
16 changes: 14 additions & 2 deletions core/xdsresource/lds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
v3 "github.com/envoyproxy/go-control-plane/envoy/type/v3"
"github.com/golang/protobuf/ptypes/any"
_struct "github.com/golang/protobuf/ptypes/struct"
wrappers "github.com/golang/protobuf/ptypes/wrappers"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/structpb"
)
Expand Down Expand Up @@ -82,6 +83,7 @@ func TestUnmarshalLDSHttpConnectionManager(t *testing.T) {
assert.Equal(t, RouteConfigName1, lis1.NetworkFilters[0].RouteConfigName)
assert.Equal(t, uint32(80), lis1.NetworkFilters[0].RoutePort)
assert.Equal(t, uint32(10), lis1.NetworkFilters[0].InlineRouteConfig.MaxTokens)
assert.Equal(t, uint32(101), lis1.NetworkFilters[0].InlineRouteConfig.TokensPerFill)

// inline route config
lis2 := res[ReturnedLisName2]
Expand Down Expand Up @@ -145,6 +147,9 @@ func TestGetLocalRateLimitFromHttpConnectionManager(t *testing.T) {
rateLimit := &ratelimitv3.LocalRateLimit{
TokenBucket: &v3.TokenBucket{
MaxTokens: 10,
TokensPerFill: &wrappers.UInt32Value{
Value: 10,
},
},
}
hcm := &v3httppb.HttpConnectionManager{
Expand All @@ -161,9 +166,10 @@ func TestGetLocalRateLimitFromHttpConnectionManager(t *testing.T) {
},
},
}
token, err := getLocalRateLimitFromHttpConnectionManager(hcm)
token, tokensPerfill, err := getLocalRateLimitFromHttpConnectionManager(hcm)
assert.Equal(t, err, nil)
assert.Equal(t, token, uint32(10))
assert.Equal(t, tokensPerfill, uint32(10))

// ---------------------------------- struct ratelimit ------------------------------------
structLimit := &udpatypev1.TypedStruct{
Expand All @@ -179,6 +185,11 @@ func TestGetLocalRateLimitFromHttpConnectionManager(t *testing.T) {
NumberValue: 100,
},
},
"tokens_per_fill": {
Kind: &structpb.Value_NumberValue{
NumberValue: 100,
},
},
},
},
},
Expand All @@ -201,7 +212,8 @@ func TestGetLocalRateLimitFromHttpConnectionManager(t *testing.T) {
},
},
}
token, err = getLocalRateLimitFromHttpConnectionManager(hcm1)
token, tokensPerfill, err = getLocalRateLimitFromHttpConnectionManager(hcm1)
assert.Equal(t, err, nil)
assert.Equal(t, token, uint32(100))
assert.Equal(t, tokensPerfill, uint32(100))
}
1 change: 1 addition & 0 deletions core/xdsresource/rds.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type RouteConfigResource struct {
HTTPRouteConfig *HTTPRouteConfig
ThriftRouteConfig *ThriftRouteConfig
MaxTokens uint32
TokensPerFill uint32
}

type HTTPRouteConfig struct {
Expand Down
3 changes: 3 additions & 0 deletions core/xdsresource/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ var (
rateLimit = &ratelimitv3.LocalRateLimit{
TokenBucket: &typedv3.TokenBucket{
MaxTokens: 10,
TokensPerFill: &wrappers.UInt32Value{
Value: 101,
},
},
}
// Rds
Expand Down
10 changes: 5 additions & 5 deletions xdssuite/circuitbreak_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/kitex-contrib/xds/core/xdsresource"
)

func cbConifg(conf interface{}) interface{} {
func cbConfig(conf interface{}) interface{} {
m := conf.(map[string]interface{})
m = m["cb_config"].(map[string]interface{})
return m["service"]
Expand All @@ -48,7 +48,7 @@ func TestCircuitBreaker(t *testing.T) {
},
},
})
assert.Equal(t, cbConifg(cb.cb.Dump()), map[string]interface{}{
assert.Equal(t, cbConfig(cb.cb.Dump()), map[string]interface{}{
"c1": circuitbreak.CBConfig{
Enable: true,
MinSample: 100,
Expand All @@ -64,7 +64,7 @@ func TestCircuitBreaker(t *testing.T) {
},
},
})
assert.Equal(t, cbConifg(cb.cb.Dump()), map[string]interface{}{
assert.Equal(t, cbConfig(cb.cb.Dump()), map[string]interface{}{
"c1": circuitbreak.CBConfig{
Enable: false,
},
Expand All @@ -81,7 +81,7 @@ func TestCircuitBreaker(t *testing.T) {
},
},
})
assert.Equal(t, cbConifg(cb.cb.Dump()), map[string]interface{}{
assert.Equal(t, cbConfig(cb.cb.Dump()), map[string]interface{}{
"c1": circuitbreak.CBConfig{
Enable: false,
},
Expand All @@ -98,7 +98,7 @@ func TestCircuitBreaker(t *testing.T) {
},
},
})
assert.Equal(t, cbConifg(cb.cb.Dump()), map[string]interface{}{
assert.Equal(t, cbConfig(cb.cb.Dump()), map[string]interface{}{
"c1": circuitbreak.CBConfig{
Enable: false,
},
Expand Down
6 changes: 3 additions & 3 deletions xdssuite/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ func getLimiterPolicy(up map[string]xdsresource.Resource) map[uint32]uint32 {
if lds == nil {
return nil
}
maxTokens := make(map[uint32]uint32)
tpfs := make(map[uint32]uint32)
for _, lis := range lds.NetworkFilters {
if lis.InlineRouteConfig != nil {
maxTokens[lis.RoutePort] = lis.InlineRouteConfig.MaxTokens
tpfs[lis.RoutePort] = lis.InlineRouteConfig.TokensPerFill
}
}
return maxTokens
return tpfs
}

type limiter struct {
Expand Down
10 changes: 5 additions & 5 deletions xdssuite/limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestLimiter(t *testing.T) {
{
RoutePort: 80,
InlineRouteConfig: &xdsresource.RouteConfigResource{
MaxTokens: 100,
TokensPerFill: 100,
},
},
},
Expand All @@ -58,7 +58,7 @@ func TestLimiter(t *testing.T) {
{
RoutePort: 80,
InlineRouteConfig: &xdsresource.RouteConfigResource{
MaxTokens: 100,
TokensPerFill: 100,
},
},
},
Expand All @@ -73,7 +73,7 @@ func TestLimiter(t *testing.T) {
{
RoutePort: 80,
InlineRouteConfig: &xdsresource.RouteConfigResource{
MaxTokens: 1000,
TokensPerFill: 1000,
},
},
},
Expand All @@ -88,7 +88,7 @@ func TestLimiter(t *testing.T) {
{
RoutePort: 0,
InlineRouteConfig: &xdsresource.RouteConfigResource{
MaxTokens: 999,
TokensPerFill: 999,
},
},
},
Expand All @@ -103,7 +103,7 @@ func TestLimiter(t *testing.T) {
{
RoutePort: 8080,
InlineRouteConfig: &xdsresource.RouteConfigResource{
MaxTokens: 999,
TokensPerFill: 999,
},
},
},
Expand Down

0 comments on commit 5c8bf68

Please sign in to comment.