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

balancer: protect rand source with a mutex #353

Merged
merged 1 commit into from Dec 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 12 additions & 5 deletions pkg/balancer/hashring.go
Expand Up @@ -2,6 +2,7 @@ package balancer

import (
"math/rand"
"sync"
"time"

"google.golang.org/grpc/balancer"
Expand All @@ -22,10 +23,7 @@ const (
CtxKey ctxKey = "requestKey"
)

var (
logger = grpclog.Component("consistenthashring")
r = rand.New(rand.NewSource(time.Now().UnixNano()))
)
var logger = grpclog.Component("consistenthashring")

// NewConsistentHashringBuilder creates a new balancer.Builder that
// will create a consistent hashring balancer with the given config.
Expand Down Expand Up @@ -75,12 +73,15 @@ func (b *consistentHashringPickerBuilder) Build(info base.PickerBuildInfo) balan
return &consistentHashringPicker{
hashring: hashring,
spread: b.spread,
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}

type consistentHashringPicker struct {
sync.Mutex
hashring *consistent.Hashring
spread uint8
rand *rand.Rand
}

func (p *consistentHashringPicker) Pick(info balancer.PickInfo) (balancer.PickResult, error) {
Expand All @@ -89,7 +90,13 @@ func (p *consistentHashringPicker) Pick(info balancer.PickInfo) (balancer.PickRe
if err != nil {
return balancer.PickResult{}, err
}
chosen := members[r.Intn(int(p.spread))].(subConnMember)

// rand is not safe for concurrent use
p.Lock()
index := p.rand.Intn(int(p.spread))
p.Unlock()

chosen := members[index].(subConnMember)
return balancer.PickResult{
SubConn: chosen.SubConn,
}, nil
Expand Down