From fa01b8d09a5427cf63a8810caf0cd9934816c571 Mon Sep 17 00:00:00 2001 From: whalecold Date: Mon, 2 Sep 2024 14:58:57 +0800 Subject: [PATCH 1/4] chore: use correct envoy field indicates limit policy --- core/manager/manager_test.go | 6 ++++-- core/xdsresource/lds.go | 24 +++++++++++++++--------- core/xdsresource/lds_test.go | 16 ++++++++++++++-- core/xdsresource/testutil.go | 3 +++ xdssuite/limiter.go | 6 +++--- xdssuite/limiter_test.go | 10 +++++----- 6 files changed, 44 insertions(+), 21 deletions(-) diff --git a/core/manager/manager_test.go b/core/manager/manager_test.go index fceb744..fcf1bcc 100644 --- a/core/manager/manager_test.go +++ b/core/manager/manager_test.go @@ -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, }, }, }, diff --git a/core/xdsresource/lds.go b/core/xdsresource/lds.go index 9c59fe0..11f3c84 100644 --- a/core/xdsresource/lds.go +++ b/core/xdsresource/lds.go @@ -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 } @@ -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 @@ -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: @@ -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 { @@ -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 } diff --git a/core/xdsresource/lds_test.go b/core/xdsresource/lds_test.go index 3331c94..a41f7c6 100644 --- a/core/xdsresource/lds_test.go +++ b/core/xdsresource/lds_test.go @@ -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" ) @@ -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] @@ -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{ @@ -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{ @@ -179,6 +185,11 @@ func TestGetLocalRateLimitFromHttpConnectionManager(t *testing.T) { NumberValue: 100, }, }, + "tokens_per_fill": { + Kind: &structpb.Value_NumberValue{ + NumberValue: 100, + }, + }, }, }, }, @@ -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)) } diff --git a/core/xdsresource/testutil.go b/core/xdsresource/testutil.go index cd450c7..c45054e 100644 --- a/core/xdsresource/testutil.go +++ b/core/xdsresource/testutil.go @@ -107,6 +107,9 @@ var ( rateLimit = &ratelimitv3.LocalRateLimit{ TokenBucket: &typedv3.TokenBucket{ MaxTokens: 10, + TokensPerFill: &wrappers.UInt32Value{ + Value: 101, + }, }, } // Rds diff --git a/xdssuite/limiter.go b/xdssuite/limiter.go index 4922884..c0ec63e 100644 --- a/xdssuite/limiter.go +++ b/xdssuite/limiter.go @@ -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 { diff --git a/xdssuite/limiter_test.go b/xdssuite/limiter_test.go index 75e6b5c..1ae422e 100644 --- a/xdssuite/limiter_test.go +++ b/xdssuite/limiter_test.go @@ -43,7 +43,7 @@ func TestLimiter(t *testing.T) { { RoutePort: 80, InlineRouteConfig: &xdsresource.RouteConfigResource{ - MaxTokens: 100, + TokensPerFill: 100, }, }, }, @@ -58,7 +58,7 @@ func TestLimiter(t *testing.T) { { RoutePort: 80, InlineRouteConfig: &xdsresource.RouteConfigResource{ - MaxTokens: 100, + TokensPerFill: 100, }, }, }, @@ -73,7 +73,7 @@ func TestLimiter(t *testing.T) { { RoutePort: 80, InlineRouteConfig: &xdsresource.RouteConfigResource{ - MaxTokens: 1000, + TokensPerFill: 1000, }, }, }, @@ -88,7 +88,7 @@ func TestLimiter(t *testing.T) { { RoutePort: 0, InlineRouteConfig: &xdsresource.RouteConfigResource{ - MaxTokens: 999, + TokensPerFill: 999, }, }, }, @@ -103,7 +103,7 @@ func TestLimiter(t *testing.T) { { RoutePort: 8080, InlineRouteConfig: &xdsresource.RouteConfigResource{ - MaxTokens: 999, + TokensPerFill: 999, }, }, }, From 7a62c3e9b70a48be5b983f2e1cccbf0c7577de8a Mon Sep 17 00:00:00 2001 From: whalecold Date: Mon, 2 Sep 2024 15:09:13 +0800 Subject: [PATCH 2/4] update readme --- README.md | 1 + README_CN.md | 1 + 2 files changed, 2 insertions(+) diff --git a/README.md b/README.md index e27e407..6b9e345 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/README_CN.md b/README_CN.md index 7aea016..3fcc4bc 100644 --- a/README_CN.md +++ b/README_CN.md @@ -250,6 +250,7 @@ spec: token_bucket: # 限流参数 max_tokens: 4 + tokens_per_fill: 10 workloadSelector: labels: # 服务实例 pod 的标签,根据实际情况填写 From 7d4ea5bb61754c7e79a33d8fb626fbf6be5632ce Mon Sep 17 00:00:00 2001 From: whalecold Date: Mon, 2 Sep 2024 16:42:39 +0800 Subject: [PATCH 3/4] fix typo --- xdssuite/circuitbreak_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/xdssuite/circuitbreak_test.go b/xdssuite/circuitbreak_test.go index 85ee8ec..3a0f48a 100644 --- a/xdssuite/circuitbreak_test.go +++ b/xdssuite/circuitbreak_test.go @@ -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"] @@ -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, @@ -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, }, @@ -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, }, @@ -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, }, From 7cf1c73b2158b629c4fb8c45a0abb6a0fe321d1c Mon Sep 17 00:00:00 2001 From: whalecold Date: Mon, 2 Sep 2024 16:48:13 +0800 Subject: [PATCH 4/4] fix typo --- core/xdsresource/rds.go | 1 + 1 file changed, 1 insertion(+) diff --git a/core/xdsresource/rds.go b/core/xdsresource/rds.go index a59c448..0ef39af 100644 --- a/core/xdsresource/rds.go +++ b/core/xdsresource/rds.go @@ -34,6 +34,7 @@ type RouteConfigResource struct { HTTPRouteConfig *HTTPRouteConfig ThriftRouteConfig *ThriftRouteConfig MaxTokens uint32 + TokensPerFill uint32 } type HTTPRouteConfig struct {