1
0
Fork 0

Make P2C strategy thread-safe

This commit is contained in:
Landry Benguigui 2025-05-19 09:52:05 +02:00 committed by GitHub
parent 79fde2b6dd
commit 3deea566ac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -58,7 +58,8 @@ type Balancer struct {
sticky *loadbalancer.Sticky
rand rnd
randMu sync.Mutex
rand rnd
}
// New creates a new power-of-two-random-choices load balancer.
@ -152,10 +153,13 @@ func (b *Balancer) nextServer() (*namedHandler, error) {
if len(healthy) == 1 {
return healthy[0], nil
}
// In order to not get the same backend twice, we make the second call to s.rand.IntN one fewer
// In order to not get the same backend twice, we make the second call to s.rand.Intn one fewer
// than the length of the slice. We then have to shift over the second index if it is equal or
// greater than the first index, wrapping round if needed.
b.randMu.Lock()
n1, n2 := b.rand.Intn(len(healthy)), b.rand.Intn(len(healthy))
b.randMu.Unlock()
if n2 == n1 {
n2 = (n2 + 1) % len(healthy)
}