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

randomWRR: remove lock for accessing WRR.items #6666

Merged
merged 5 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 3 additions & 4 deletions internal/wrr/wrr.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ 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. All Add must be only called
// before any calls to Next().
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nits:

s/All//

s/Next()/Next/

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly for clarity: "Add may not be called after any call to Next."

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Next() any
}
Loading