Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade golang version to 1.20 #161

Merged
merged 3 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 9 additions & 24 deletions dbdrivers/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/retail-ai-inc/bean

go 1.18
go 1.20

require (
github.com/getsentry/sentry-go v0.13.0
Expand Down
6 changes: 1 addition & 5 deletions helpers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package helpers
import (
"math/rand"
"os"
"time"

str "github.com/retail-ai-inc/bean/string"
)
Expand All @@ -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
}

Expand Down
27 changes: 7 additions & 20 deletions helpers/jitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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)
}