From 514d35a95ae7c5143d815bd697c312d9b978ca5c Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Tue, 24 Sep 2024 11:51:39 +0800 Subject: [PATCH] case insensitive and delete config when QPS and concurrency set to 0 Signed-off-by: Ryan Leung --- pkg/ratelimit/option.go | 6 +++++- server/api/router.go | 2 +- server/api/service_middleware.go | 2 ++ server/api/service_middleware_test.go | 16 ++++++++-------- server/server.go | 6 +++--- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/pkg/ratelimit/option.go b/pkg/ratelimit/option.go index f1faac5b5508..a2f474c3ff1d 100644 --- a/pkg/ratelimit/option.go +++ b/pkg/ratelimit/option.go @@ -78,7 +78,11 @@ func UpdateDimensionConfig(cfg *DimensionConfig) Option { return InAllowList } lim, _ := l.limiters.LoadOrStore(label, newLimiter()) - return lim.(*limiter).updateDimensionConfig(cfg) + status := lim.(*limiter).updateDimensionConfig(cfg) + if status&QPSDeleted != 0 && status&ConcurrencyDeleted != 0 { + l.limiters.Delete(label) + } + return status } } diff --git a/server/api/router.go b/server/api/router.go index 0e129706b43d..185281c9f983 100644 --- a/server/api/router.go +++ b/server/api/router.go @@ -389,7 +389,7 @@ func createRouter(prefix string, svr *server.Server) *mux.Router { rootRouter.HandleFunc("/ping", func(http.ResponseWriter, *http.Request) {}).Methods(http.MethodGet) _ = rootRouter.Walk(func(route *mux.Route, _ *mux.Router, _ []*mux.Route) error { - serviceLabel := route.GetName() + serviceLabel := strings.ToLower(route.GetName()) methods, _ := route.GetMethods() path, _ := route.GetPathTemplate() if len(serviceLabel) == 0 { diff --git a/server/api/service_middleware.go b/server/api/service_middleware.go index ecd41eede08e..d9f3e2642e06 100644 --- a/server/api/service_middleware.go +++ b/server/api/service_middleware.go @@ -159,6 +159,7 @@ func (h *serviceMiddlewareHandler) SetRateLimitConfig(w http.ResponseWriter, r * h.rd.JSON(w, http.StatusBadRequest, "The label is empty.") return } + serviceLabel = strings.ToLower(serviceLabel) if len(h.svr.GetServiceLabels(serviceLabel)) == 0 { h.rd.JSON(w, http.StatusBadRequest, "There is no label matched.") return @@ -248,6 +249,7 @@ func (h *serviceMiddlewareHandler) SetGRPCRateLimitConfig(w http.ResponseWriter, h.rd.JSON(w, http.StatusBadRequest, "The label is empty.") return } + serviceLabel = strings.ToLower(serviceLabel) if !h.svr.IsGRPCServiceLabelExist(serviceLabel) { h.rd.JSON(w, http.StatusBadRequest, "There is no label matched.") return diff --git a/server/api/service_middleware_test.go b/server/api/service_middleware_test.go index 7d5e0db98be1..d4b5c4d5f390 100644 --- a/server/api/service_middleware_test.go +++ b/server/api/service_middleware_test.go @@ -240,7 +240,7 @@ func (suite *rateLimitConfigTestSuite) TestUpdateRateLimitConfig() { err = tu.CheckPostJSON(testDialClient, urlPrefix, jsonBody, tu.StatusOK(re), tu.StringContain(re, "QPS rate limiter is changed.")) re.NoError(err) - re.Equal(1, suite.svr.GetRateLimitConfig().LimiterConfig["GetHealthStatus"].QPSBurst) + re.Equal(1, suite.svr.GetRateLimitConfig().LimiterConfig["gethealthstatus"].QPSBurst) input["qps"] = -1 jsonBody, err = json.Marshal(input) @@ -263,9 +263,9 @@ func (suite *rateLimitConfigTestSuite) TestUpdateRateLimitConfig() { tu.StringContain(re, "QPS rate limiter is changed."), tu.ExtractJSON(re, &result), ) - re.Equal(100., result.LimiterConfig["Profile"].QPS) - re.Equal(100, result.LimiterConfig["Profile"].QPSBurst) - re.Equal(uint64(100), result.LimiterConfig["Profile"].ConcurrencyLimit) + re.Equal(100., result.LimiterConfig["profile"].QPS) + re.Equal(100, result.LimiterConfig["profile"].QPSBurst) + re.Equal(uint64(100), result.LimiterConfig["profile"].ConcurrencyLimit) re.NoError(err) limiter := suite.svr.GetServiceRateLimiter() @@ -370,9 +370,9 @@ func (suite *rateLimitConfigTestSuite) TestUpdateGRPCRateLimitConfig() { tu.StringContain(re, "QPS rate limiter is changed."), tu.ExtractJSON(re, &result), ) - re.Equal(100., result.LimiterConfig["GetStore"].QPS) - re.Equal(100, result.LimiterConfig["GetStore"].QPSBurst) - re.Equal(uint64(100), result.LimiterConfig["GetStore"].ConcurrencyLimit) + re.Equal(100., result.LimiterConfig["getStore"].QPS) + re.Equal(100, result.LimiterConfig["getStore"].QPSBurst) + re.Equal(uint64(100), result.LimiterConfig["getStore"].ConcurrencyLimit) re.NoError(err) } @@ -451,5 +451,5 @@ func (suite *rateLimitConfigTestSuite) TestConfigLimiterConfigByOriginAPI() { re.NoError(tu.CheckPostJSON(testDialClient, addr, postData, tu.StatusOK(re))) sc := &config.ServiceMiddlewareConfig{} re.NoError(tu.ReadGetJSON(re, testDialClient, addr, sc)) - re.Equal(1., sc.RateLimitConfig.LimiterConfig["CreateOperator"].QPS) + re.Equal(1., sc.RateLimitConfig.LimiterConfig["createoperator"].QPS) } diff --git a/server/server.go b/server/server.go index c79f51d81530..0fde374c1c06 100644 --- a/server/server.go +++ b/server/server.go @@ -369,7 +369,7 @@ func (s *Server) initGRPCServiceLabels() { for name, serviceInfo := range s.grpcServer.GetServiceInfo() { if name == gRPCServiceName { for _, methodInfo := range serviceInfo.Methods { - s.grpcServiceLabels[methodInfo.Name] = struct{}{} + s.grpcServiceLabels[strings.ToLower(methodInfo.Name)] = struct{}{} } } } @@ -1458,7 +1458,7 @@ func (s *Server) GetRegions() []*core.RegionInfo { // GetServiceLabels returns ApiAccessPaths by given service label // TODO: this function will be used for updating api rate limit config func (s *Server) GetServiceLabels(serviceLabel string) []apiutil.AccessPath { - if apis, ok := s.serviceLabels[serviceLabel]; ok { + if apis, ok := s.serviceLabels[strings.ToLower(serviceLabel)]; ok { return apis } return nil @@ -1466,7 +1466,7 @@ func (s *Server) GetServiceLabels(serviceLabel string) []apiutil.AccessPath { // IsGRPCServiceLabelExist returns if the service label exists func (s *Server) IsGRPCServiceLabelExist(serviceLabel string) bool { - _, ok := s.grpcServiceLabels[serviceLabel] + _, ok := s.grpcServiceLabels[strings.ToLower(serviceLabel)] return ok }