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

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

merged 1 commit into from
Sep 2, 2015

Conversation

jamesgroat
Copy link
Contributor

Currently there is a data race when a consumer hits a backoff condition and then resumes. The resume via time.AfterFunc gets called from a different goroutine.

Note: this could also have been solved using one of the existing mutexes (r.mtx or r.backoffMtx).

@@ -1004,6 +1009,8 @@ func (r *Consumer) redistributeRDY() {
availableMaxInFlight = 1 - atomic.LoadInt64(&r.totalRdyCount)
}

r.rngMtx.Lock()
defer r.rngMtx.Unlock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we reduce the size of this critical path and put this lock around just the call to r.rng.Int()?

@mreiferson
Copy link
Member

thanks for this, just one minor comment!

@jamesgroat
Copy link
Contributor Author

I've addressed the comment.

@mreiferson
Copy link
Member

LGTM, do you mind squashing down to one commit?

@@ -106,7 +106,8 @@ type Consumer struct {
channel string
config Config

rng *rand.Rand
rngMtx sync.RWMutex
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RWMutex is designed for separate read and write locks. Since we only want global locking can you use sync.Mutex here?

@jamesgroat
Copy link
Contributor Author

Squashed and changed to using sync.Mutex.

mreiferson added a commit that referenced this pull request Sep 2, 2015
Add mutex around rng *rand.Rand to avoid data race on resume
@mreiferson mreiferson merged commit 383c07c into nsqio:master Sep 2, 2015
@jamesgroat jamesgroat deleted the consumer-rng-race branch September 2, 2015 20:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants