1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-29 17:41:15 +03:00

Reduce dependency chain

This commit is contained in:
Dimitrij Denissenko
2020-09-17 14:13:43 +01:00
parent 2eb40d83aa
commit 3fc96195a4
10 changed files with 59 additions and 64 deletions

View File

@ -3,7 +3,7 @@ package hashtag
import (
"strings"
"golang.org/x/exp/rand"
"github.com/go-redis/redis/v8/internal/rand"
)
const slotNumber = 16384

View File

@ -3,9 +3,10 @@ package hashtag
import (
"testing"
"github.com/go-redis/redis/v8/internal/rand"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"golang.org/x/exp/rand"
)
func TestGinkgoSuite(t *testing.T) {
@ -40,8 +41,8 @@ var _ = Describe("HashSlot", func() {
{"{}foo", 9500},
{"foo{}", 5542},
{"foo{}{bar}", 8363},
{"", 7404},
{"", 4403},
{"", 10503},
{"", 5176},
{string([]byte{83, 153, 134, 118, 229, 214, 244, 75, 140, 37, 215, 215}), 5463},
}
// Empty keys receive random slot.

View File

@ -3,7 +3,7 @@ package internal
import (
"time"
"golang.org/x/exp/rand"
"github.com/go-redis/redis/v8/internal/rand"
)
func RetryBackoff(retry int, minBackoff, maxBackoff time.Duration) time.Duration {

45
internal/rand/rand.go Normal file
View File

@ -0,0 +1,45 @@
package rand
import (
"math/rand"
"sync"
)
// Int returns a non-negative pseudo-random int.
func Int() int { return pseudo.Int() }
// Intn returns, as an int, a non-negative pseudo-random number in [0,n).
// It panics if n <= 0.
func Intn(n int) int { return pseudo.Intn(n) }
// Int63n returns, as an int64, a non-negative pseudo-random number in [0,n).
// It panics if n <= 0.
func Int63n(n int64) int64 { return pseudo.Int63n(n) }
// Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n).
func Perm(n int) []int { return pseudo.Perm(n) }
// Seed uses the provided seed value to initialize the default Source to a
// deterministic state. If Seed is not called, the generator behaves as if
// seeded by Seed(1).
func Seed(n int64) { pseudo.Seed(n) }
var pseudo = rand.New(&source{src: rand.NewSource(1)})
type source struct {
src rand.Source
mu sync.Mutex
}
func (s *source) Int63() int64 {
s.mu.Lock()
n := s.src.Int63()
s.mu.Unlock()
return n
}
func (s *source) Seed(seed int64) {
s.mu.Lock()
s.src.Seed(seed)
s.mu.Unlock()
}