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

chore: Ratelimiter is enabled by default #271

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions server/config/config.go
Original file line number Diff line number Diff line change
@@ -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,
12 changes: 6 additions & 6 deletions server/limiter/limiter.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
2 changes: 1 addition & 1 deletion server/service/http/api.go
Original file line number Diff line number Diff line change
@@ -371,9 +371,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 {
2 changes: 1 addition & 1 deletion server/service/http/types.go
Original file line number Diff line number Diff line change
@@ -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 UpdateDeployModeRequest struct {