Skip to content

Commit

Permalink
feat: update PR
Browse files Browse the repository at this point in the history
  • Loading branch information
SoulPancake committed Jun 1, 2024
1 parent 7ac3f0e commit 8ee282a
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 28 deletions.
4 changes: 2 additions & 2 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
6 changes: 1 addition & 5 deletions internal/util/rand.1.22.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
6 changes: 1 addition & 5 deletions internal/util/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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())
Expand Down
16 changes: 2 additions & 14 deletions internal/util/rand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion rueidislock/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 8ee282a

Please sign in to comment.