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

Use a private random number generator #41

Merged
merged 1 commit into from
Jun 2, 2014
Merged
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
11 changes: 6 additions & 5 deletions consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (
"time"
)

// Use a private rng so as not to mess with client application's seeds
var rng = rand.New(rand.NewSource(time.Now().UnixNano()))

// Handler is the message processing interface for Consumer
//
// Implement this interface for handlers that return whether or not message
Expand Down Expand Up @@ -220,9 +223,7 @@ func (r *Consumer) ConnectToNSQLookupd(addr string) error {
func (r *Consumer) lookupdLoop() {
// add some jitter so that multiple consumers discovering the same topic,
// when restarted at the same time, dont all connect at once.
rand.Seed(time.Now().UnixNano())

jitter := time.Duration(int64(rand.Float64() *
jitter := time.Duration(int64(rng.Float64() *
r.config.lookupdPollJitter * float64(r.config.lookupdPollInterval)))
ticker := time.NewTicker(r.config.lookupdPollInterval)

Expand Down Expand Up @@ -529,7 +530,7 @@ func (r *Consumer) rdyLoop() {
if len(conns) == 0 {
continue
}
idx := rand.Intn(len(conns))
idx := rng.Intn(len(conns))
for _, c := range conns {
if i == idx {
choice = c
Expand Down Expand Up @@ -732,7 +733,7 @@ func (r *Consumer) redistributeRDY() {

for len(possibleConns) > 0 && availableMaxInFlight > 0 {
availableMaxInFlight--
i := rand.Int() % len(possibleConns)
i := rng.Int() % len(possibleConns)
c := possibleConns[i]
// delete
possibleConns = append(possibleConns[:i], possibleConns[i+1:]...)
Expand Down