Repository: spidernet-io/spiderpool
Pull request #779 (2022-09-28 13:45:45)

Set defaults for limiter config
Signed-off-by: iiiceoo <ziqian.xue@daocloud.io>

sonarcloud[bot]: Kudos, SonarCloud Quality Gate passed!&nbsp; &nbsp; [![Quality Gate passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/passed-16px.png 'Quality Gate passed')](https://sonarcloud.io/dashboard?id=spidernet-io_spiderpool&pullRequest=779)

[![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=spidernet-io_spiderpool&pullRequest=779&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=spidernet-io_spiderpool&pullRequest=779&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=spidernet-io_spiderpool&pullRequest=779&resolved=false&types=BUG)  
[![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=spidernet-io_spiderpool&pullRequest=779&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=spidernet-io_spiderpool&pullRequest=779&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=spidernet-io_spiderpool&pullRequest=779&resolved=false&types=VULNERABILITY)  
[![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=spidernet-io_spiderpool&pullRequest=779&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=spidernet-io_spiderpool&pullRequest=779&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=spidernet-io_spiderpool&pullRequest=779&resolved=false&types=SECURITY_HOTSPOT)  
[![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=spidernet-io_spiderpool&pullRequest=779&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=spidernet-io_spiderpool&pullRequest=779&resolved=false&types=CODE_SMELL) [0 Code Smells](https://sonarcloud.io/project/issues?id=spidernet-io_spiderpool&pullRequest=779&resolved=false&types=CODE_SMELL)

[![No Coverage information](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/NoCoverageInfo-16px.png 'No Coverage information')](https://sonarcloud.io/component_measures?id=spidernet-io_spiderpool&pullRequest=779) No Coverage information  
[![0.5%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3-16px.png '0.5%')](https://sonarcloud.io/component_measures?id=spidernet-io_spiderpool&pullRequest=779&metric=new_duplicated_lines_density&view=list) [0.5% Duplication](https://sonarcloud.io/component_measures?id=spidernet-io_spiderpool&pullRequest=779&metric=new_duplicated_lines_density&view=list)



Patch:
diff --git a/cmd/spiderpool-agent/cmd/daemon.go b/cmd/spiderpool-agent/cmd/daemon.go
index b195a93fe..cab781368 100644
--- a/cmd/spiderpool-agent/cmd/daemon.go
+++ b/cmd/spiderpool-agent/cmd/daemon.go
@@ -107,6 +107,7 @@ func DaemonMain() {
 	initAgentServiceManagers(agentContext.InnerCtx)
 
 	logger.Info("Begin to initialize IPAM")
+	limiterMaxWaitTime := time.Duration(agentContext.Cfg.LimiterMaxWaitTime) * time.Second
 	ipam, err := ipam.NewIPAM(&ipam.IPAMConfig{
 		EnableIPv4:               agentContext.Cfg.EnableIPv4,
 		EnableIPv6:               agentContext.Cfg.EnableIPv6,
@@ -114,9 +115,9 @@ func DaemonMain() {
 		ClusterDefaultIPv6IPPool: agentContext.Cfg.ClusterDefaultIPv6IPPool,
 		EnableSpiderSubnet:       agentContext.Cfg.EnableSpiderSubnet,
 		EnableStatefulSet:        agentContext.Cfg.EnableStatefulSet,
-		LimiterConfig: &limiter.LimiterConfig{
-			MaxQueueSize: agentContext.Cfg.LimiterMaxQueueSize,
-			MaxWaitTime:  time.Duration(agentContext.Cfg.LimiterMaxWaitTime) * time.Second,
+		LimiterConfig: limiter.LimiterConfig{
+			MaxQueueSize: &agentContext.Cfg.LimiterMaxQueueSize,
+			MaxWaitTime:  &limiterMaxWaitTime,
 		},
 		WaitSubnetPoolRetries: agentContext.Cfg.UpdateCRMaxRetrys,
 		WaitSubnetPoolTime:    time.Duration(agentContext.Cfg.WaitSubnetPoolTime) * time.Second,
diff --git a/pkg/ipam/config.go b/pkg/ipam/config.go
index f8881a887..86da799bf 100644
--- a/pkg/ipam/config.go
+++ b/pkg/ipam/config.go
@@ -24,10 +24,9 @@ type IPAMConfig struct {
 	EnableSpiderSubnet bool
 	EnableStatefulSet  bool
 
-	LimiterConfig *limiter.LimiterConfig
-
 	WaitSubnetPoolRetries int
 	WaitSubnetPoolTime    time.Duration
+	LimiterConfig         limiter.LimiterConfig
 }
 
 func (c *IPAMConfig) getClusterDefaultPool(ctx context.Context, nic string, cleanGateway bool) (*ToBeAllocated, error) {
diff --git a/pkg/limiter/config.go b/pkg/limiter/config.go
index a4e7d39dd..58d5768c7 100644
--- a/pkg/limiter/config.go
+++ b/pkg/limiter/config.go
@@ -5,7 +5,25 @@ package limiter
 
 import "time"
 
+const (
+	defaultMaxQueueSize = 1000
+	defaultMaxWaitTime  = 15 * time.Second
+)
+
 type LimiterConfig struct {
-	MaxQueueSize int
-	MaxWaitTime  time.Duration
+	MaxQueueSize *int
+	MaxWaitTime  *time.Duration
+}
+
+func setDefaultsForLimiterConfig(config LimiterConfig) LimiterConfig {
+	maxQueueSize, maxWaitTime := defaultMaxQueueSize, defaultMaxWaitTime
+	if config.MaxQueueSize == nil {
+		config.MaxQueueSize = &maxQueueSize
+	}
+
+	if config.MaxWaitTime == nil {
+		config.MaxWaitTime = &maxWaitTime
+	}
+
+	return config
 }
diff --git a/pkg/limiter/queue.go b/pkg/limiter/queue.go
index 12b5da17c..bba532b20 100644
--- a/pkg/limiter/queue.go
+++ b/pkg/limiter/queue.go
@@ -20,12 +20,14 @@ type Limiter interface {
 	Start(ctx context.Context) error
 }
 
-func NewLimiter(c *LimiterConfig) Limiter {
+func NewLimiter(c LimiterConfig) Limiter {
+	c = setDefaultsForLimiterConfig(c)
+
 	q := &queue{
 		cond:           sync.NewCond(&lock.Mutex{}),
-		maxQueueSize:   c.MaxQueueSize,
-		maxWaitTime:    c.MaxWaitTime,
-		elements:       make([]*e, 0, c.MaxQueueSize),
+		maxQueueSize:   *c.MaxQueueSize,
+		maxWaitTime:    *c.MaxWaitTime,
+		elements:       make([]*e, 0, *c.MaxQueueSize),
 		grantedTickets: map[string]int{},
 	}
 


Test patch:
diff --git a/pkg/limiter/queue_test.go b/pkg/limiter/queue_test.go
index 2e78278d6..5e5b492eb 100644
--- a/pkg/limiter/queue_test.go
+++ b/pkg/limiter/queue_test.go
@@ -19,7 +19,7 @@ import (
 var _ = Describe("Queue", Label("unitest", "queue_test"), func() {
 	var ctx context.Context
 	var cancel context.CancelFunc
-	var config *limiter.LimiterConfig
+	var config limiter.LimiterConfig
 	var queue limiter.Limiter
 
 	var queuers int
@@ -38,9 +38,12 @@ var _ = Describe("Queue", Label("unitest", "queue_test"), func() {
 		BeforeEach(func() {
 			ctx, cancel = context.WithCancel(context.Background())
 			DeferCleanup(cancel)
-			config = &limiter.LimiterConfig{
-				MaxQueueSize: 3,
-				MaxWaitTime:  2 * time.Second,
+
+			maxQueueSize := 3
+			maxWaitTime := 2 * time.Second
+			config = limiter.LimiterConfig{
+				MaxQueueSize: &maxQueueSize,
+				MaxWaitTime:  &maxWaitTime,
 			}
 			queuers = 3
 			workHours = 1 * time.Second
@@ -108,9 +111,11 @@ var _ = Describe("Queue", Label("unitest", "queue_test"), func() {
 			ctx, cancel = context.WithCancel(context.Background())
 			DeferCleanup(cancel)
 
-			config = &limiter.LimiterConfig{
-				MaxQueueSize: 200,
-				MaxWaitTime:  5 * time.Second,
+			maxQueueSize := 200
+			maxWaitTime := 5 * time.Second
+			config = limiter.LimiterConfig{
+				MaxQueueSize: &maxQueueSize,
+				MaxWaitTime:  &maxWaitTime,
 			}
 			queuers = 200
 			workHours = 50 * time.Millisecond
@@ -170,9 +175,11 @@ var _ = Describe("Queue", Label("unitest", "queue_test"), func() {
 			ctx, cancel = context.WithCancel(context.Background())
 			DeferCleanup(cancel)
 
-			config = &limiter.LimiterConfig{
-				MaxQueueSize: 3,
-				MaxWaitTime:  2 * time.Second,
+			maxQueueSize := 3
+			maxWaitTime := 2 * time.Second
+			config = limiter.LimiterConfig{
+				MaxQueueSize: &maxQueueSize,
+				MaxWaitTime:  &maxWaitTime,
 			}
 			queuers = 3
 			workHours = 1 * time.Second