Skip to content

Commit

Permalink
config: fix Clone in RatelimitConfig (#7289)
Browse files Browse the repository at this point in the history
close #7288

Signed-off-by: Cabinfever_B <[email protected]>
  • Loading branch information
CabinfeverB authored Oct 31, 2023
1 parent ce0bb5d commit ded917b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
26 changes: 26 additions & 0 deletions server/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/BurntSushi/toml"
"github.com/spf13/pflag"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/ratelimit"
sc "github.com/tikv/pd/pkg/schedule/config"
"github.com/tikv/pd/pkg/storage"
"github.com/tikv/pd/pkg/utils/configutil"
Expand Down Expand Up @@ -479,3 +480,28 @@ func newTestScheduleOption() (*PersistOptions, error) {
opt := NewPersistOptions(cfg)
return opt, nil
}

func TestRateLimitClone(t *testing.T) {
re := require.New(t)
cfg := &RateLimitConfig{
EnableRateLimit: defaultEnableRateLimitMiddleware,
LimiterConfig: make(map[string]ratelimit.DimensionConfig),
}
clone := cfg.Clone()
clone.LimiterConfig["test"] = ratelimit.DimensionConfig{
ConcurrencyLimit: 200,
}
dc := cfg.LimiterConfig["test"]
re.Equal(dc.ConcurrencyLimit, uint64(0))

gCfg := &GRPCRateLimitConfig{
EnableRateLimit: defaultEnableGRPCRateLimitMiddleware,
LimiterConfig: make(map[string]ratelimit.DimensionConfig),
}
gClone := gCfg.Clone()
gClone.LimiterConfig["test"] = ratelimit.DimensionConfig{
ConcurrencyLimit: 300,
}
gdc := gCfg.LimiterConfig["test"]
re.Equal(gdc.ConcurrencyLimit, uint64(0))
}
10 changes: 10 additions & 0 deletions server/config/service_middleware_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ type RateLimitConfig struct {

// Clone returns a cloned rate limit config.
func (c *RateLimitConfig) Clone() *RateLimitConfig {
m := make(map[string]ratelimit.DimensionConfig, len(c.LimiterConfig))
for k, v := range c.LimiterConfig {
m[k] = v
}
cfg := *c
cfg.LimiterConfig = m
return &cfg
}

Expand All @@ -92,6 +97,11 @@ type GRPCRateLimitConfig struct {

// Clone returns a cloned rate limit config.
func (c *GRPCRateLimitConfig) Clone() *GRPCRateLimitConfig {
m := make(map[string]ratelimit.DimensionConfig, len(c.LimiterConfig))
for k, v := range c.LimiterConfig {
m[k] = v
}
cfg := *c
cfg.LimiterConfig = m
return &cfg
}

0 comments on commit ded917b

Please sign in to comment.