Skip to content

Commit 59f57b1

Browse files
authored
randomWRR: remove lock for accessing WRR.items (#6666)
1 parent afaf31a commit 59f57b1

File tree

2 files changed

+4
-10
lines changed

2 files changed

+4
-10
lines changed

internal/wrr/random.go

-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package wrr
2020
import (
2121
"fmt"
2222
"sort"
23-
"sync"
2423

2524
"google.golang.org/grpc/internal/grpcrand"
2625
)
@@ -38,7 +37,6 @@ func (w *weightedItem) String() string {
3837

3938
// randomWRR is a struct that contains weighted items implement weighted random algorithm.
4039
type randomWRR struct {
41-
mu sync.RWMutex
4240
items []*weightedItem
4341
// Are all item's weights equal
4442
equalWeights bool
@@ -52,8 +50,6 @@ func NewRandom() WRR {
5250
var grpcrandInt63n = grpcrand.Int63n
5351

5452
func (rw *randomWRR) Next() (item any) {
55-
rw.mu.RLock()
56-
defer rw.mu.RUnlock()
5753
if len(rw.items) == 0 {
5854
return nil
5955
}
@@ -72,8 +68,6 @@ func (rw *randomWRR) Next() (item any) {
7268
}
7369

7470
func (rw *randomWRR) Add(item any, weight int64) {
75-
rw.mu.Lock()
76-
defer rw.mu.Unlock()
7771
accumulatedWeight := weight
7872
equalWeights := true
7973
if len(rw.items) > 0 {

internal/wrr/wrr.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ package wrr
2121

2222
// WRR defines an interface that implements weighted round robin.
2323
type WRR interface {
24-
// Add adds an item with weight to the WRR set.
25-
//
26-
// Add and Next need to be thread safe.
24+
// Add adds an item with weight to the WRR set. Add must be only called
25+
// before any calls to Next.
2726
Add(item any, weight int64)
2827
// Next returns the next picked item.
2928
//
30-
// Add and Next need to be thread safe.
29+
// Next needs to be thread safe. Add may not be called after any call to
30+
// Next.
3131
Next() any
3232
}

0 commit comments

Comments
 (0)