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

Add mutex around rng *rand.Rand to avoid data race on resume #156

Merged
merged 1 commit into from
Sep 2, 2015
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
9 changes: 8 additions & 1 deletion consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ type Consumer struct {
channel string
config Config

rng *rand.Rand
rngMtx sync.Mutex
rng *rand.Rand

needRDYRedistributed int32

Expand Down Expand Up @@ -369,8 +370,10 @@ func validatedLookupAddr(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.
r.rngMtx.Lock()
jitter := time.Duration(int64(r.rng.Float64() *
r.config.LookupdPollJitter * float64(r.config.LookupdPollInterval)))
r.rngMtx.Unlock()
var ticker *time.Ticker

select {
Expand Down Expand Up @@ -830,7 +833,9 @@ func (r *Consumer) resume() {
r.backoff(time.Second)
return
}
r.rngMtx.Lock()
idx := r.rng.Intn(len(conns))
r.rngMtx.Unlock()
choice := conns[idx]

r.log(LogLevelWarning,
Expand Down Expand Up @@ -1006,7 +1011,9 @@ func (r *Consumer) redistributeRDY() {

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