Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
chore: Ratelimiter is enabled by default (#271)
Browse files Browse the repository at this point in the history
## Rationale
Ratelimiter is enabled by default to prevent ceresmeta from being hung.

## Detailed Changes
Ratelimiter is enabled by default.

## Test Plan
CI.
  • Loading branch information
chunshao90 authored Nov 17, 2023
1 parent 8a070ad commit 849c585
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -294,9 +294,9 @@ func MakeConfigParser() (*Parser, error) {
File: log.DefaultLogFile,
},
FlowLimiter: LimiterConfig{
Enable: defaultEnableLimiter,
Limit: defaultInitialLimiterRate,
Burst: defaultInitialLimiterCapacity,
Enable: defaultEnableLimiter,
},

EnableEmbedEtcd: defaultEnableEmbedEtcd,
Expand Down
12 changes: 6 additions & 6 deletions server/limiter/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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,
}
}
2 changes: 1 addition & 1 deletion server/service/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion server/service/http/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 849c585

Please sign in to comment.