From 849c5853d4b21ac43ecc71573936fc8a6d26a56a Mon Sep 17 00:00:00 2001 From: "chunshao.rcs" Date: Fri, 17 Nov 2023 15:13:47 +0800 Subject: [PATCH] chore: Ratelimiter is enabled by default (#271) ## Rationale Ratelimiter is enabled by default to prevent ceresmeta from being hung. ## Detailed Changes Ratelimiter is enabled by default. ## Test Plan CI. --- server/config/config.go | 8 ++++---- server/limiter/limiter.go | 12 ++++++------ server/service/http/api.go | 2 +- server/service/http/types.go | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/server/config/config.go b/server/config/config.go index 6913faf3..f127e3d7 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -38,9 +38,9 @@ const ( defaultEtcdKeyPath = "" defaultEtcdCertPath = "" + defaultEnableLimiter bool = true defaultInitialLimiterCapacity int = 100 * 1000 defaultInitialLimiterRate int = 10 * 1000 - defaultEnableLimiter bool = false defaultEtcdStartTimeoutMs int64 = 60 * 1000 defaultCallTimeoutMs = 5 * 1000 defaultEtcdMaxTxnOps = 128 @@ -95,12 +95,12 @@ const ( ) type LimiterConfig struct { + // Enable is used to control the switch of the limiter. + Enable bool `toml:"enable" env:"FLOW_LIMITER_ENABLE"` // Limit is the updated rate of tokens. Limit int `toml:"limit" env:"FLOW_LIMITER_LIMIT"` // Burst is the maximum number of tokens. Burst int `toml:"burst" env:"FLOW_LIMITER_BURST"` - // Enable is used to control the switch of the limiter. - Enable bool `toml:"enable" env:"FLOW_LIMITER_ENABLE"` } // Config is server start config, it has three input modes: @@ -294,9 +294,9 @@ func MakeConfigParser() (*Parser, error) { File: log.DefaultLogFile, }, FlowLimiter: LimiterConfig{ + Enable: defaultEnableLimiter, Limit: defaultInitialLimiterRate, Burst: defaultInitialLimiterCapacity, - Enable: defaultEnableLimiter, }, EnableEmbedEtcd: defaultEnableEmbedEtcd, diff --git a/server/limiter/limiter.go b/server/limiter/limiter.go index 20d64f0e..9fc5ac78 100644 --- a/server/limiter/limiter.go +++ b/server/limiter/limiter.go @@ -24,26 +24,26 @@ import ( ) type FlowLimiter struct { - l *rate.Limiter + // enable is used to control the switch of the limiter. + enable bool + l *rate.Limiter // RWMutex is used to protect following fields. lock sync.RWMutex // limit is the updated rate of tokens. limit int // burst is the maximum number of tokens. burst int - // enable is used to control the switch of the limiter. - enable bool } func NewFlowLimiter(config config.LimiterConfig) *FlowLimiter { newLimiter := rate.NewLimiter(rate.Limit(config.Limit), config.Burst) return &FlowLimiter{ + enable: config.Enable, l: newLimiter, lock: sync.RWMutex{}, limit: config.Limit, burst: config.Burst, - enable: config.Enable, } } @@ -58,18 +58,18 @@ func (f *FlowLimiter) UpdateLimiter(config config.LimiterConfig) error { f.lock.Lock() defer f.lock.Unlock() + f.enable = config.Enable f.l.SetLimit(rate.Limit(config.Limit)) f.l.SetBurst(config.Burst) f.limit = config.Limit f.burst = config.Burst - f.enable = config.Enable return nil } func (f *FlowLimiter) GetConfig() *config.LimiterConfig { return &config.LimiterConfig{ + Enable: f.enable, Limit: f.limit, Burst: f.burst, - Enable: f.enable, } } diff --git a/server/service/http/api.go b/server/service/http/api.go index b7f70f4d..52bf7a14 100644 --- a/server/service/http/api.go +++ b/server/service/http/api.go @@ -370,9 +370,9 @@ func (a *API) updateFlowLimiter(req *http.Request) apiFuncResult { log.Info("update flow limiter request", zap.String("request", fmt.Sprintf("%+v", updateFlowLimiterRequest))) newLimiterConfig := config.LimiterConfig{ + Enable: updateFlowLimiterRequest.Enable, Limit: updateFlowLimiterRequest.Limit, Burst: updateFlowLimiterRequest.Burst, - Enable: updateFlowLimiterRequest.Enable, } if err := a.flowLimiter.UpdateLimiter(newLimiterConfig); err != nil { diff --git a/server/service/http/types.go b/server/service/http/types.go index 20e91cda..8ca24e8e 100644 --- a/server/service/http/types.go +++ b/server/service/http/types.go @@ -148,9 +148,9 @@ type UpdateClusterRequest struct { } type UpdateFlowLimiterRequest struct { + Enable bool `json:"enable"` Limit int `json:"limit"` Burst int `json:"burst"` - Enable bool `json:"enable"` } type UpdateEnableScheduleRequest struct {