Skip to content

Commit aac1437

Browse files
committedJun 2, 2014
Merge pull request nsqio#10 from hailocab/randseed
Use private random number generator
2 parents 05cec3c + f0ebfc0 commit aac1437

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed
 

‎reader.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import (
1717
log "github.com/cihub/seelog"
1818
)
1919

20+
// Use a private rng so as not to mess with client application's seeds
21+
var rng = rand.New(rand.NewSource(time.Now().UnixNano()))
22+
2023
// Handler is the synchronous interface to Reader.
2124
//
2225
// Implement this interface for handlers that return whether or not message
@@ -204,7 +207,6 @@ func (q *Reader) conns() []*Conn {
204207
return conns
205208
}
206209

207-
208210
// ConnectionMaxInFlight calculates the per-connection max-in-flight count.
209211
//
210212
// This may change dynamically based on the number of connections to nsqd the Reader
@@ -301,9 +303,8 @@ func (q *Reader) ConnectToLookupd(addr string) error {
301303
func (q *Reader) lookupdLoop() {
302304
// add some jitter so that multiple consumers discovering the same topic,
303305
// when restarted at the same time, dont all connect at once.
304-
rand.Seed(time.Now().UnixNano())
305306

306-
jitter := time.Duration(int64(rand.Float64() * q.LookupdPollJitter * float64(q.LookupdPollInterval)))
307+
jitter := time.Duration(int64(rng.Float64() * q.LookupdPollJitter * float64(q.LookupdPollInterval)))
307308
ticker := time.NewTicker(q.LookupdPollInterval)
308309

309310
select {
@@ -671,7 +672,7 @@ func (q *Reader) rdyLoop() {
671672
if len(q.connections) == 0 {
672673
continue
673674
}
674-
idx := rand.Intn(len(q.connections))
675+
idx := rng.Intn(len(q.connections))
675676
for _, c := range q.connections {
676677
if i == idx {
677678
choice = c
@@ -884,7 +885,7 @@ func (q *Reader) redistributeRDY() {
884885

885886
for len(possibleConns) > 0 && availableMaxInFlight > 0 {
886887
availableMaxInFlight--
887-
i := rand.Int() % len(possibleConns)
888+
i := rng.Int() % len(possibleConns)
888889
c := possibleConns[i]
889890
// delete
890891
possibleConns = append(possibleConns[:i], possibleConns[i+1:]...)

0 commit comments

Comments
 (0)
Please sign in to comment.