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

perf: Reuse rand for shuffling queues #916

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 6 additions & 2 deletions processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ type processor struct {

starting chan<- *workerInfo
finished chan<- *base.TaskMessage

// queueShuffleRand holds the random source to shuffle the queues.
queueShuffleRand *rand.Rand
}

type processorParams struct {
Expand Down Expand Up @@ -119,6 +122,7 @@ func newProcessor(params processorParams) *processor {
shutdownTimeout: params.shutdownTimeout,
starting: params.starting,
finished: params.finished,
queueShuffleRand: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}

Expand Down Expand Up @@ -404,8 +408,8 @@ 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] })
p.queueShuffleRand.Seed(time.Now().UnixNano())
p.queueShuffleRand.Shuffle(len(names), func(i, j int) { names[i], names[j] = names[j], names[i] })
return uniq(names, len(p.queueConfig))
}

Expand Down
Loading