refactor(math): use Go1.21's built-in min/max function (#3656)

Signed-off-by: Xiaolong Chen <[email protected]>
This commit is contained in:
cxljs
2026-01-08 17:46:13 +02:00
committed by GitHub
parent 389fd3ea95
commit 254e65fb47
6 changed files with 19 additions and 45 deletions
+2 -4
View File
@@ -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(),
+1 -2
View File
@@ -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
-17
View File
@@ -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
}
+5 -6
View File
@@ -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)
+8 -13
View File
@@ -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)
+3 -3
View File
@@ -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)
}