Skip to content

Commit 3c0c5ed

Browse files
committed
Add mutex around rng *rand.Rand to avoid data race on resume
1 parent 04ffac3 commit 3c0c5ed

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

consumer.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ type Consumer struct {
106106
channel string
107107
config Config
108108

109-
rng *rand.Rand
109+
rngMtx sync.Mutex
110+
rng *rand.Rand
110111

111112
needRDYRedistributed int32
112113

@@ -369,8 +370,10 @@ func validatedLookupAddr(addr string) error {
369370
func (r *Consumer) lookupdLoop() {
370371
// add some jitter so that multiple consumers discovering the same topic,
371372
// when restarted at the same time, dont all connect at once.
373+
r.rngMtx.Lock()
372374
jitter := time.Duration(int64(r.rng.Float64() *
373375
r.config.LookupdPollJitter * float64(r.config.LookupdPollInterval)))
376+
r.rngMtx.Unlock()
374377
var ticker *time.Ticker
375378

376379
select {
@@ -830,7 +833,9 @@ func (r *Consumer) resume() {
830833
r.backoff(time.Second)
831834
return
832835
}
836+
r.rngMtx.Lock()
833837
idx := r.rng.Intn(len(conns))
838+
r.rngMtx.Unlock()
834839
choice := conns[idx]
835840

836841
r.log(LogLevelWarning,
@@ -1006,7 +1011,9 @@ func (r *Consumer) redistributeRDY() {
10061011

10071012
for len(possibleConns) > 0 && availableMaxInFlight > 0 {
10081013
availableMaxInFlight--
1014+
r.rngMtx.Lock()
10091015
i := r.rng.Int() % len(possibleConns)
1016+
r.rngMtx.Unlock()
10101017
c := possibleConns[i]
10111018
// delete
10121019
possibleConns = append(possibleConns[:i], possibleConns[i+1:]...)

0 commit comments

Comments
 (0)