From ea2a5c728f3084af7d1bab8e33f916fc128f3fd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=92=9F=E5=A4=A7=E9=99=86?= <10188142zhong_dalu@cn.tre-inc.com> Date: Tue, 26 Dec 2023 13:58:18 +0800 Subject: [PATCH 1/3] 1.upgrade golang version to 1.21 2.improve `rand` related --- dbdrivers/redis.go | 33 +++++++++------------------------ dbdrivers/redis_test.go | 16 ++++++++++++++++ go.mod | 2 +- helpers/common.go | 6 +----- helpers/jitter.go | 27 +++++++-------------------- 5 files changed, 34 insertions(+), 50 deletions(-) diff --git a/dbdrivers/redis.go b/dbdrivers/redis.go index a0a9bf7c..9de83c0a 100644 --- a/dbdrivers/redis.go +++ b/dbdrivers/redis.go @@ -155,9 +155,7 @@ func RedisGetString(c context.Context, clients *RedisDBConn, key string) (str st } } else if clients.readCount > 1 { // Select a read replica between 0 ~ noOfReadReplica-1 randomly. - // TODO: Use global seed and make go version as 1.20 minimum. - rng := rand.New(rand.NewSource(time.Now().UnixNano())) - readHost := rng.Intn(clients.readCount) + readHost := rand.Intn(clients.readCount) str, err = clients.Read[uint64(readHost)].Get(c, key).Result() if err != nil { @@ -193,9 +191,7 @@ func RedisMGet(c context.Context, clients *RedisDBConn, keys ...string) (result } } else if clients.readCount > 1 { // Select a read replica between 0 ~ noOfReadReplica-1 randomly. - // TODO: Use global seed and make go version as 1.20 minimum. - rng := rand.New(rand.NewSource(time.Now().UnixNano())) - readHost := rng.Intn(clients.readCount) + readHost := rand.Intn(clients.readCount) result, err = clients.Read[uint64(readHost)].MGet(c, keys...).Result() if err != nil { @@ -229,9 +225,7 @@ func RedisHGet(c context.Context, clients *RedisDBConn, key string, field string } } else if clients.readCount > 1 { // Select a read replica between 0 ~ noOfReadReplica-1 randomly. - // TODO: Use global seed and make go version as 1.20 minimum. - rng := rand.New(rand.NewSource(time.Now().UnixNano())) - readHost := rng.Intn(clients.readCount) + readHost := rand.Intn(clients.readCount) result, err = clients.Read[uint64(readHost)].HGet(c, key, field).Result() if err != nil { @@ -267,9 +261,7 @@ func RedisHgets(c context.Context, clients *RedisDBConn, redisKeysWithField map[ pipe = clients.Read[0].Pipeline() } else if clients.readCount > 1 { // Select a read replica between 0 ~ noOfReadReplica-1 randomly. - // TODO: Use global seed and make go version as 1.20 minimum. - rng := rand.New(rand.NewSource(time.Now().UnixNano())) - readHost := rng.Intn(clients.readCount) + readHost := rand.Intn(clients.readCount) pipe = clients.Read[uint64(readHost)].Pipeline() } else { // If there is no read replica then just hit the host server. @@ -313,9 +305,7 @@ func RedisGetLRange(c context.Context, clients *RedisDBConn, key string, start, } } else if clients.readCount > 1 { // Select a read replica between 0 ~ noOfReadReplica-1 randomly. - // TODO: Use global seed and make go version as 1.20 minimum. - rng := rand.New(rand.NewSource(time.Now().UnixNano())) - readHost := rng.Intn(clients.readCount) + readHost := rand.Intn(clients.readCount) str, err = clients.Read[uint64(readHost)].LRange(c, key, start, stop).Result() if err != nil { @@ -350,9 +340,7 @@ func RedisSMembers(c context.Context, clients *RedisDBConn, key string) (str []s } } else if clients.readCount > 1 { // Select a read replica between 0 ~ noOfReadReplica-1 randomly. - // TODO: Use global seed and make go version as 1.20 minimum. - rng := rand.New(rand.NewSource(time.Now().UnixNano())) - readHost := rng.Intn(clients.readCount) + readHost := rand.Intn(clients.readCount) str, err = clients.Read[uint64(readHost)].SMembers(c, key).Result() if err != nil { @@ -387,9 +375,7 @@ func RedisSIsMember(c context.Context, clients *RedisDBConn, key string, element } } else if clients.readCount > 1 { // Select a read replica between 0 ~ noOfReadReplica-1 randomly. - // TODO: Use global seed and make go version as 1.20 minimum. - rng := rand.New(rand.NewSource(time.Now().UnixNano())) - readHost := rng.Intn(clients.readCount) + readHost := rand.Intn(clients.readCount) found, err = clients.Read[uint64(readHost)].SIsMember(c, key, element).Result() if err != nil { @@ -422,9 +408,7 @@ func RedisSRandMemberN(c context.Context, clients *RedisDBConn, key string, coun } } else if clients.readCount > 1 { // Select a read replica between 0 ~ noOfReadReplica-1 randomly. - // TODO: Use global seed and make go version as 1.20 minimum. - rng := rand.New(rand.NewSource(time.Now().UnixNano())) - readHost := rng.Intn(clients.readCount) + readHost := rand.Intn(clients.readCount) result, err = clients.Read[uint64(readHost)].SRandMemberN(c, key, count).Result() if err != nil { @@ -532,6 +516,7 @@ func RedisExpireKey(c context.Context, clients *RedisDBConn, key string, ttl tim // - RedisMSet("key1", "value1", "key2", "value2") // - RedisMSet([]string{"key1", "value1", "key2", "value2"}) // - RedisMSet(map[string]interface{}{"key1": "value1", "key2": "value2"}) +// // For `struct` values, please implement the `encoding.BinaryMarshaler` interface. func RedisMSet(c context.Context, clients *RedisDBConn, values ...interface{}) (err error) { if clients.isCluster { diff --git a/dbdrivers/redis_test.go b/dbdrivers/redis_test.go index d4e21f17..a555af0c 100644 --- a/dbdrivers/redis_test.go +++ b/dbdrivers/redis_test.go @@ -1,6 +1,9 @@ package dbdrivers import ( + "fmt" + "math/rand" + "sync" "testing" "time" @@ -52,3 +55,16 @@ func Test_connectRedisDB(t *testing.T) { }) } } + +func TestRand(t *testing.T) { + var wg sync.WaitGroup + var count = 10000 + wg.Add(count) + for i := 0; i < count; i++ { + go func() { + fmt.Printf("%d,", rand.Intn(count)) + wg.Done() + }() + } + wg.Wait() +} diff --git a/go.mod b/go.mod index 9ff2ab34..010ad863 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/retail-ai-inc/bean -go 1.18 +go 1.21 require ( github.com/getsentry/sentry-go v0.13.0 diff --git a/helpers/common.go b/helpers/common.go index c787080d..2602d4e6 100644 --- a/helpers/common.go +++ b/helpers/common.go @@ -25,7 +25,6 @@ package helpers import ( "math/rand" "os" - "time" str "github.com/retail-ai-inc/bean/string" ) @@ -35,10 +34,7 @@ type CopyableSlice []interface{} // GetRandomNumberFromRange will generate and return a random integer from a range. func GetRandomNumberFromRange(min, max int) int { - - // TODO: Use global seed and make go version as 1.20 minimum. - rng := rand.New(rand.NewSource(time.Now().UnixNano())) - n := min + rng.Intn(max-min+1) + n := min + rand.Intn(max-min+1) return n } diff --git a/helpers/jitter.go b/helpers/jitter.go index 5f8e50fc..54d707c9 100644 --- a/helpers/jitter.go +++ b/helpers/jitter.go @@ -3,13 +3,9 @@ package helpers import ( "math" "math/rand" - "sync" "time" ) -var rnd = newRnd() -var rndMu sync.Mutex - // JitterBackoff Return capped exponential backoff with jitter. It is useful for http client when you want to retry request. // http://www.awsarchitectureblog.com/2015/03/backoff.html @@ -28,12 +24,12 @@ var rndMu sync.Mutex // waitTime := helpers.JitterBackoff(time.Duration(100) * time.Millisecond, time.Duration(2000) * time.Millisecond, i) -// select { -// case <-time.After(waitTime): -// case <-c.Done(): -// return c.Err() -// } -// } +// select { +// case <-time.After(waitTime): +// case <-c.Done(): +// return c.Err() +// } +// } func JitterBackoff(min, max time.Duration, attempt int) time.Duration { base := float64(min) capLevel := float64(max) @@ -50,16 +46,7 @@ func JitterBackoff(min, max time.Duration, attempt int) time.Duration { } func randDuration(center time.Duration) time.Duration { - rndMu.Lock() - defer rndMu.Unlock() - var ri = int64(center) - var jitter = rnd.Int63n(ri) + var jitter = rand.Int63n(ri) return time.Duration(math.Abs(float64(ri + jitter))) } - -func newRnd() *rand.Rand { - var seed = time.Now().UnixNano() - var src = rand.NewSource(seed) - return rand.New(src) -} From cd65eaeacc6f603331028f99350d9b0b6b50a8d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=92=9F=E5=A4=A7=E9=99=86?= <10188142zhong_dalu@cn.tre-inc.com> Date: Tue, 26 Dec 2023 16:09:42 +0800 Subject: [PATCH 2/3] code brush --- dbdrivers/redis_test.go | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/dbdrivers/redis_test.go b/dbdrivers/redis_test.go index a555af0c..d4e21f17 100644 --- a/dbdrivers/redis_test.go +++ b/dbdrivers/redis_test.go @@ -1,9 +1,6 @@ package dbdrivers import ( - "fmt" - "math/rand" - "sync" "testing" "time" @@ -55,16 +52,3 @@ func Test_connectRedisDB(t *testing.T) { }) } } - -func TestRand(t *testing.T) { - var wg sync.WaitGroup - var count = 10000 - wg.Add(count) - for i := 0; i < count; i++ { - go func() { - fmt.Printf("%d,", rand.Intn(count)) - wg.Done() - }() - } - wg.Wait() -} From 277057a565a199692db5d99b5c3f795ae745d22e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=92=9F=E5=A4=A7=E9=99=86?= <10188142zhong_dalu@cn.tre-inc.com> Date: Wed, 27 Dec 2023 12:39:24 +0800 Subject: [PATCH 3/3] change version to 1.20 --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 010ad863..28645e3c 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/retail-ai-inc/bean -go 1.21 +go 1.20 require ( github.com/getsentry/sentry-go v0.13.0