From 63f7cb7b1765b4161a45251d763281532282262a Mon Sep 17 00:00:00 2001 From: Pior Bastida Date: Mon, 28 Oct 2024 11:04:06 +0100 Subject: [PATCH] Use math/rand/v2 --- processor.go | 7 +++---- server.go | 5 ++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/processor.go b/processor.go index 4ebbe2e..fa810d6 100644 --- a/processor.go +++ b/processor.go @@ -8,7 +8,7 @@ import ( "context" "fmt" "math" - "math/rand" + "math/rand/v2" "runtime" "runtime/debug" "sort" @@ -181,7 +181,7 @@ func (p *processor) exec() { // Sleep to avoid slamming redis and let scheduler move tasks into queues. // Note: We are not using blocking pop operation and polling queues instead. // This adds significant load to redis. - jitter := time.Duration(rand.Intn(int(p.taskCheckInterval))) + jitter := rand.N(p.taskCheckInterval) time.Sleep(p.taskCheckInterval/2 + jitter) <-p.sema // release token return @@ -413,8 +413,7 @@ func (p *processor) queues() []string { names = append(names, qname) } } - r := rand.New(rand.NewSource(time.Now().UnixNano())) - r.Shuffle(len(names), func(i, j int) { names[i], names[j] = names[j], names[i] }) + rand.Shuffle(len(names), func(i, j int) { names[i], names[j] = names[j], names[i] }) return uniq(names, len(p.queueConfig)) } diff --git a/server.go b/server.go index 5e7f48b..0cc4f38 100644 --- a/server.go +++ b/server.go @@ -9,7 +9,7 @@ import ( "errors" "fmt" "math" - "math/rand" + "math/rand/v2" "runtime" "strings" "sync" @@ -400,9 +400,8 @@ func toInternalLogLevel(l LogLevel) log.Level { // DefaultRetryDelayFunc is the default RetryDelayFunc used if one is not specified in Config. // It uses exponential back-off strategy to calculate the retry delay. func DefaultRetryDelayFunc(n int, e error, t *Task) time.Duration { - r := rand.New(rand.NewSource(time.Now().UnixNano())) // Formula taken from https://github.com/mperham/sidekiq. - s := int(math.Pow(float64(n), 4)) + 15 + (r.Intn(30) * (n + 1)) + s := int(math.Pow(float64(n), 4)) + 15 + (rand.IntN(30) * (n + 1)) return time.Duration(s) * time.Second }