diff --git a/cluster.go b/cluster.go index f8f2d18e..a5ec8e66 100644 --- a/cluster.go +++ b/cluster.go @@ -272,7 +272,7 @@ func (c *clusterClient) _refresh() (err error) { nodesCount := len(g.nodes) for _, slot := range g.slots { for i := slot[0]; i <= slot[1]; i++ { - pslots[i] = conns[g.nodes[1+util.IntN(nodesCount-1)]].conn + pslots[i] = conns[g.nodes[1+util.FastRand(nodesCount-1)]].conn } } case c.rOpt != nil: // implies c.opt.SendToReplicas != nil @@ -283,7 +283,7 @@ func (c *clusterClient) _refresh() (err error) { for _, slot := range g.slots { for i := slot[0]; i <= slot[1]; i++ { pslots[i] = conns[master].conn - rslots[i] = conns[g.nodes[1+util.IntN(len(g.nodes)-1)]].conn + rslots[i] = conns[g.nodes[1+util.FastRand(len(g.nodes)-1)]].conn } } } else { diff --git a/internal/util/rand.1.22.go b/internal/util/rand.1.22.go index 687ca293..87dbcc95 100644 --- a/internal/util/rand.1.22.go +++ b/internal/util/rand.1.22.go @@ -12,15 +12,11 @@ func Shuffle(n int, swap func(i, j int)) { rand.Shuffle(n, swap) } -func IntN(n int) int { - return rand.IntN(n) -} - func FastRand(n int) int { return rand.IntN(n) } -func BinaryString() []byte { +func RandomBytes() []byte { val := make([]byte, 24) binary.BigEndian.PutUint64(val[0:8], rand.Uint64()) binary.BigEndian.PutUint64(val[8:16], rand.Uint64()) diff --git a/internal/util/rand.go b/internal/util/rand.go index a197f482..8ec00c4b 100644 --- a/internal/util/rand.go +++ b/internal/util/rand.go @@ -20,10 +20,6 @@ func Shuffle(n int, swap func(i, j int)) { rand.Shuffle(n, swap) } -func IntN(n int) int { - return rand.Intn(n) -} - var rngPool = sync.Pool{ New: func() any { return rand.New(rand.NewSource(time.Now().UnixNano())) @@ -37,7 +33,7 @@ func FastRand(n int) (r int) { return } -func BinaryString() []byte { +func RandomBytes() []byte { val := make([]byte, 24) src := sources.Get().(rand.Source64) binary.BigEndian.PutUint64(val[0:8], src.Uint64()) diff --git a/internal/util/rand_test.go b/internal/util/rand_test.go index be45f8c4..12589a76 100644 --- a/internal/util/rand_test.go +++ b/internal/util/rand_test.go @@ -4,7 +4,6 @@ import ( "testing" ) -// TestShuffle tests the Shuffle function. func TestShuffle(t *testing.T) { arr := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} Shuffle(len(arr), func(i, j int) { @@ -15,16 +14,6 @@ func TestShuffle(t *testing.T) { } } -// TestIntN tests the IntN function. -func TestIntN(t *testing.T) { - n := 10 - res := IntN(n) - if res < 0 || res >= n { - t.Errorf("Expected result between 0 and %d, got %d", n-1, res) - } -} - -// TestFastRand tests the FastRand function. func TestFastRand(t *testing.T) { n := 10 res := FastRand(n) @@ -33,9 +22,8 @@ func TestFastRand(t *testing.T) { } } -// TestRandomBinaryString tests the RandomBinaryString function. -func TestRandomBinaryString(t *testing.T) { - val := BinaryString() +func TestRandomBytes(t *testing.T) { + val := RandomBytes() if len(val) != 24 { t.Errorf("Expected length 24, got %d", len(val)) } diff --git a/rueidislock/lock.go b/rueidislock/lock.go index 7d051b0b..3b211414 100644 --- a/rueidislock/lock.go +++ b/rueidislock/lock.go @@ -133,7 +133,7 @@ func makegate(size int32) *gate { } func random() string { - return rueidis.BinaryString(util.BinaryString()) + return rueidis.BinaryString(util.RandomBytes()) } func keyname(prefix, name string, i int32) string { diff --git a/sentinel.go b/sentinel.go index 7881d5a3..c8b7ae14 100644 --- a/sentinel.go +++ b/sentinel.go @@ -435,7 +435,7 @@ func pickReplica(resp []RedisResult) (string, error) { } // choose a replica randomly - m := eligible[util.IntN(len(eligible))] + m := eligible[util.FastRand(len(eligible))] return net.JoinHostPort(m["ip"], m["port"]), nil }