2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-25 23:32:17 +08:00

Use math/rand/v2

This commit is contained in:
Pior Bastida 2024-10-28 11:04:06 +01:00
parent 013190b824
commit 63f7cb7b17
No known key found for this signature in database
GPG Key ID: 02A5B11A024126BC
2 changed files with 5 additions and 7 deletions

View File

@ -8,7 +8,7 @@ import (
"context" "context"
"fmt" "fmt"
"math" "math"
"math/rand" "math/rand/v2"
"runtime" "runtime"
"runtime/debug" "runtime/debug"
"sort" "sort"
@ -181,7 +181,7 @@ func (p *processor) exec() {
// Sleep to avoid slamming redis and let scheduler move tasks into queues. // Sleep to avoid slamming redis and let scheduler move tasks into queues.
// Note: We are not using blocking pop operation and polling queues instead. // Note: We are not using blocking pop operation and polling queues instead.
// This adds significant load to redis. // 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) time.Sleep(p.taskCheckInterval/2 + jitter)
<-p.sema // release token <-p.sema // release token
return return
@ -413,8 +413,7 @@ func (p *processor) queues() []string {
names = append(names, qname) names = append(names, qname)
} }
} }
r := rand.New(rand.NewSource(time.Now().UnixNano())) rand.Shuffle(len(names), func(i, j int) { names[i], names[j] = names[j], names[i] })
r.Shuffle(len(names), func(i, j int) { names[i], names[j] = names[j], names[i] })
return uniq(names, len(p.queueConfig)) return uniq(names, len(p.queueConfig))
} }

View File

@ -9,7 +9,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math" "math"
"math/rand" "math/rand/v2"
"runtime" "runtime"
"strings" "strings"
"sync" "sync"
@ -400,9 +400,8 @@ func toInternalLogLevel(l LogLevel) log.Level {
// DefaultRetryDelayFunc is the default RetryDelayFunc used if one is not specified in Config. // DefaultRetryDelayFunc is the default RetryDelayFunc used if one is not specified in Config.
// It uses exponential back-off strategy to calculate the retry delay. // It uses exponential back-off strategy to calculate the retry delay.
func DefaultRetryDelayFunc(n int, e error, t *Task) time.Duration { 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. // 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 return time.Duration(s) * time.Second
} }