Skip to content

Commit

Permalink
randomWRR: remove lock for accessing WRR.items (#6666)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvindbr8 authored Oct 7, 2023
1 parent afaf31a commit 59f57b1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
6 changes: 0 additions & 6 deletions internal/wrr/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package wrr
import (
"fmt"
"sort"
"sync"

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

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

func (rw *randomWRR) Next() (item any) {
rw.mu.RLock()
defer rw.mu.RUnlock()
if len(rw.items) == 0 {
return nil
}
Expand All @@ -72,8 +68,6 @@ func (rw *randomWRR) Next() (item any) {
}

func (rw *randomWRR) Add(item any, weight int64) {
rw.mu.Lock()
defer rw.mu.Unlock()
accumulatedWeight := weight
equalWeights := true
if len(rw.items) > 0 {
Expand Down
8 changes: 4 additions & 4 deletions internal/wrr/wrr.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ package wrr

// WRR defines an interface that implements weighted round robin.
type WRR interface {
// Add adds an item with weight to the WRR set.
//
// Add and Next need to be thread safe.
// Add adds an item with weight to the WRR set. Add must be only called
// before any calls to Next.
Add(item any, weight int64)
// Next returns the next picked item.
//
// Add and Next need to be thread safe.
// Next needs to be thread safe. Add may not be called after any call to
// Next.
Next() any
}

0 comments on commit 59f57b1

Please sign in to comment.