Skip to content

Commit

Permalink
case insensitive and delete config when QPS and concurrency set to 0
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <[email protected]>
  • Loading branch information
rleungx committed Sep 24, 2024
1 parent edb43c0 commit 514d35a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
6 changes: 5 additions & 1 deletion pkg/ratelimit/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions server/api/service_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions server/api/service_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}
6 changes: 3 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}
}
}
}
Expand Down Expand Up @@ -1458,15 +1458,15 @@ 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
}

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

Expand Down

0 comments on commit 514d35a

Please sign in to comment.