From c8be2091e682c27808b6f528bd0603bf1381dfa7 Mon Sep 17 00:00:00 2001 From: Xiaolong Chen Date: Mon, 22 Dec 2025 12:22:10 +0800 Subject: [PATCH] use Go1.21's built-in min/max function Signed-off-by: Xiaolong Chen --- command.go | 6 ++---- internal/pool/pool.go | 3 +-- internal/util/math.go | 17 ----------------- maintnotifications/config.go | 11 +++++------ maintnotifications/config_test.go | 21 ++++++++------------- sentinel_test.go | 6 +++--- 6 files changed, 19 insertions(+), 45 deletions(-) delete mode 100644 internal/util/math.go diff --git a/command.go b/command.go index a4a0e76326..ea65692527 100644 --- a/command.go +++ b/command.go @@ -4,6 +4,7 @@ import ( "bufio" "context" "fmt" + "maps" "net" "regexp" "strconv" @@ -2003,10 +2004,7 @@ func (cmd *StringStructMapCmd) readReply(rd *proto.Reader) error { func (cmd *StringStructMapCmd) Clone() Cmder { var val map[string]struct{} if cmd.val != nil { - val = make(map[string]struct{}, len(cmd.val)) - for k := range cmd.val { - val[k] = struct{}{} - } + val = maps.Clone(cmd.val) } return &StringStructMapCmd{ baseCmd: cmd.cloneBaseCmd(), diff --git a/internal/pool/pool.go b/internal/pool/pool.go index 77cecc646c..c13c7b17ab 100644 --- a/internal/pool/pool.go +++ b/internal/pool/pool.go @@ -10,7 +10,6 @@ import ( "github.com/redis/go-redis/v9/internal" "github.com/redis/go-redis/v9/internal/proto" - "github.com/redis/go-redis/v9/internal/util" ) var ( @@ -701,7 +700,7 @@ func (p *ConnPool) popIdle() (*Conn, error) { var cn *Conn attempts := 0 - maxAttempts := util.Min(popAttempts, n) + maxAttempts := min(popAttempts, n) for attempts < maxAttempts { if len(p.idleConns) == 0 { return nil, nil diff --git a/internal/util/math.go b/internal/util/math.go deleted file mode 100644 index e707c47a64..0000000000 --- a/internal/util/math.go +++ /dev/null @@ -1,17 +0,0 @@ -package util - -// Max returns the maximum of two integers -func Max(a, b int) int { - if a > b { - return a - } - return b -} - -// Min returns the minimum of two integers -func Min(a, b int) int { - if a < b { - return a - } - return b -} diff --git a/maintnotifications/config.go b/maintnotifications/config.go index cbf4f6b22b..db666f3a5b 100644 --- a/maintnotifications/config.go +++ b/maintnotifications/config.go @@ -9,7 +9,6 @@ import ( "github.com/redis/go-redis/v9/internal" "github.com/redis/go-redis/v9/internal/maintnotifications/logs" - "github.com/redis/go-redis/v9/internal/util" ) // Mode represents the maintenance notifications mode @@ -261,10 +260,10 @@ func (c *Config) ApplyDefaultsWithPoolConfig(poolSize int, maxActiveConns int) * // Default: max(20x workers, PoolSize), capped by maxActiveConns or 5x pool size workerBasedSize := result.MaxWorkers * 20 poolBasedSize := poolSize - result.HandoffQueueSize = util.Max(workerBasedSize, poolBasedSize) + result.HandoffQueueSize = max(workerBasedSize, poolBasedSize) if c.HandoffQueueSize > 0 { // When explicitly set: enforce minimum of 200 - result.HandoffQueueSize = util.Max(200, c.HandoffQueueSize) + result.HandoffQueueSize = max(200, c.HandoffQueueSize) } // Cap queue size: use maxActiveConns+1 if set, otherwise 5x pool size @@ -278,7 +277,7 @@ func (c *Config) ApplyDefaultsWithPoolConfig(poolSize int, maxActiveConns int) * } else { queueCap = poolSize * 5 } - result.HandoffQueueSize = util.Min(result.HandoffQueueSize, queueCap) + result.HandoffQueueSize = min(result.HandoffQueueSize, queueCap) // Ensure minimum queue size of 2 (fallback for very small pools) if result.HandoffQueueSize < 2 { @@ -353,10 +352,10 @@ func (c *Config) applyWorkerDefaults(poolSize int) { // When not set: min(poolSize/2, max(10, poolSize/3)) - balanced scaling approach originalMaxWorkers := c.MaxWorkers - c.MaxWorkers = util.Min(poolSize/2, util.Max(10, poolSize/3)) + c.MaxWorkers = min(poolSize/2, max(10, poolSize/3)) if originalMaxWorkers != 0 { // When explicitly set: max(poolSize/2, set_value) - ensure at least poolSize/2 workers - c.MaxWorkers = util.Max(poolSize/2, originalMaxWorkers) + c.MaxWorkers = max(poolSize/2, originalMaxWorkers) } // Ensure minimum of 1 worker (fallback for very small pools) diff --git a/maintnotifications/config_test.go b/maintnotifications/config_test.go index f02057e73f..a1e664eaed 100644 --- a/maintnotifications/config_test.go +++ b/maintnotifications/config_test.go @@ -5,8 +5,6 @@ import ( "net" "testing" "time" - - "github.com/redis/go-redis/v9/internal/util" ) func TestConfig(t *testing.T) { @@ -130,8 +128,8 @@ func TestApplyDefaults(t *testing.T) { workerBasedSize := result.MaxWorkers * 20 poolSize := 100 // Default pool size used in ApplyDefaults poolBasedSize := poolSize - expectedQueueSize := util.Max(workerBasedSize, poolBasedSize) - expectedQueueSize = util.Min(expectedQueueSize, poolSize*5) // Cap by 5x pool size + expectedQueueSize := max(workerBasedSize, poolBasedSize) + expectedQueueSize = min(expectedQueueSize, poolSize*5) // Cap by 5x pool size if result.HandoffQueueSize != expectedQueueSize { t.Errorf("Expected HandoffQueueSize to be %d (max(20*MaxWorkers=%d, poolSize=%d) capped by 5*poolSize=%d), got %d", expectedQueueSize, workerBasedSize, poolBasedSize, poolSize*5, result.HandoffQueueSize) @@ -155,8 +153,8 @@ func TestApplyDefaults(t *testing.T) { workerBasedSize := result.MaxWorkers * 20 poolSize := 100 // Default pool size used in ApplyDefaults poolBasedSize := poolSize - expectedQueueSize := util.Max(workerBasedSize, poolBasedSize) - expectedQueueSize = util.Min(expectedQueueSize, poolSize*5) // Cap by 5x pool size + expectedQueueSize := max(workerBasedSize, poolBasedSize) + expectedQueueSize = min(expectedQueueSize, poolSize*5) // Cap by 5x pool size if result.HandoffQueueSize != expectedQueueSize { t.Errorf("Expected HandoffQueueSize to be %d (max(20*MaxWorkers=%d, poolSize=%d) capped by 5*poolSize=%d), got %d", expectedQueueSize, workerBasedSize, poolBasedSize, poolSize*5, result.HandoffQueueSize) @@ -224,8 +222,8 @@ func TestApplyDefaults(t *testing.T) { workerBasedSize := result.MaxWorkers * 20 poolSize := 100 // Default pool size used in ApplyDefaults poolBasedSize := poolSize - expectedQueueSize := util.Max(workerBasedSize, poolBasedSize) - expectedQueueSize = util.Min(expectedQueueSize, poolSize*5) // Cap by 5x pool size + expectedQueueSize := max(workerBasedSize, poolBasedSize) + expectedQueueSize = min(expectedQueueSize, poolSize*5) // Cap by 5x pool size if result.HandoffQueueSize != expectedQueueSize { t.Errorf("Expected HandoffQueueSize to be %d (max(20*MaxWorkers=%d, poolSize=%d) capped by 5*poolSize=%d), got %d", expectedQueueSize, workerBasedSize, poolBasedSize, poolSize*5, result.HandoffQueueSize) @@ -235,7 +233,6 @@ func TestApplyDefaults(t *testing.T) { t.Errorf("Expected RelaxedTimeout to be 10s (default), got %v", result.RelaxedTimeout) } - }) } @@ -325,14 +322,12 @@ func TestIntegrationWithApplyDefaults(t *testing.T) { t.Errorf("Expected MaxWorkers to be 50, got %d", expectedConfig.MaxWorkers) } - - // Should apply defaults for missing fields (auto-calculated queue size with hybrid scaling) workerBasedSize := expectedConfig.MaxWorkers * 20 poolSize := 100 // Default pool size used in ApplyDefaults poolBasedSize := poolSize - expectedQueueSize := util.Max(workerBasedSize, poolBasedSize) - expectedQueueSize = util.Min(expectedQueueSize, poolSize*5) // Cap by 5x pool size + expectedQueueSize := max(workerBasedSize, poolBasedSize) + expectedQueueSize = min(expectedQueueSize, poolSize*5) // Cap by 5x pool size if expectedConfig.HandoffQueueSize != expectedQueueSize { t.Errorf("Expected HandoffQueueSize to be %d (max(20*MaxWorkers=%d, poolSize=%d) capped by 5*poolSize=%d), got %d", expectedQueueSize, workerBasedSize, poolBasedSize, poolSize*5, expectedConfig.HandoffQueueSize) diff --git a/sentinel_test.go b/sentinel_test.go index 0f0f61ebf7..c6217f6a2b 100644 --- a/sentinel_test.go +++ b/sentinel_test.go @@ -5,7 +5,7 @@ import ( "crypto/tls" "errors" "net" - "sort" + "slices" "testing" "time" @@ -671,8 +671,8 @@ func compareFailoverOptions(t *testing.T, a, e *redis.FailoverOptions) { } func compareSlices(t *testing.T, a, b []string, name string) { - sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) - sort.Slice(b, func(i, j int) bool { return b[i] < b[j] }) + slices.Sort(a) + slices.Sort(b) if len(a) != len(b) { t.Errorf("%s got %q, want %q", name, a, b) }