Skip to content

Commit a45ad7e

Browse files
committed
Review comments #1.
1 parent 29bf324 commit a45ad7e

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

balancer/weightedroundrobin/weightedroundrobin.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,35 @@
1919
// Package weightedroundrobin defines a weighted roundrobin balancer.
2020
package weightedroundrobin
2121

22-
import "google.golang.org/grpc/attributes"
22+
import (
23+
"google.golang.org/grpc/resolver"
24+
)
2325

24-
const (
25-
// Name is the name of weighted_round_robin balancer.
26-
Name = "weighted_round_robin"
26+
// Name is the name of weighted_round_robin balancer.
27+
const Name = "weighted_round_robin"
2728

28-
// Attribute key used to store AddrInfo in the Attributes field of
29-
// resolver.Address.
30-
attributeKey = "/balancer/weightedroundrobin/addrInfo"
31-
)
29+
// attributeKey is the type used as the key to store AddrInfo in the Attributes
30+
// field of resolver.Address.
31+
type attributeKey struct{}
3232

33-
// AddrInfo will be stored inside Address metadata in order to use weighted roundrobin
34-
// balancer.
33+
// AddrInfo will be stored inside Address metadata in order to use weighted
34+
// roundrobin balancer.
3535
type AddrInfo struct {
3636
Weight uint32
3737
}
3838

39-
// AddAddrInfoToAttributes returns a new Attributes containing all key/value
40-
// pairs in a with ai being added to it.
41-
func AddAddrInfoToAttributes(ai *AddrInfo, a *attributes.Attributes) *attributes.Attributes {
42-
return a.WithValues(attributeKey, ai)
39+
// SetAddrInfo sets addInfo in the Attributes field of addr.
40+
func SetAddrInfo(addrInfo *AddrInfo, addr *resolver.Address) {
41+
addr.Attributes = addr.Attributes.WithValues(attributeKey{}, addrInfo)
4342
}
4443

45-
// GetAddrInfoFromAttributes returns the AddrInfo stored in a. Returns nil if no
46-
// AddrInfo is present in a.
47-
func GetAddrInfoFromAttributes(a *attributes.Attributes) *AddrInfo {
48-
if a == nil {
44+
// GetAddrInfo returns the AddrInfo stored in the Attributes fields of addr.
45+
// Returns nil if no AddrInfo is present.
46+
func GetAddrInfo(addr *resolver.Address) *AddrInfo {
47+
if addr == nil || addr.Attributes == nil {
4948
return nil
5049
}
51-
ai := a.Value(attributeKey)
50+
ai := addr.Attributes.Value(attributeKey{})
5251
if ai == nil {
5352
return nil
5453
}

balancer/weightedroundrobin/weightedwoundrobin_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
"testing"
2323

2424
"github.com/google/go-cmp/cmp"
25-
2625
"google.golang.org/grpc/attributes"
26+
"google.golang.org/grpc/resolver"
2727
)
2828

2929
func TestAddAddrInfoToAndFromAttributes(t *testing.T) {
@@ -61,8 +61,9 @@ func TestAddAddrInfoToAndFromAttributes(t *testing.T) {
6161

6262
for _, test := range tests {
6363
t.Run(test.desc, func(t *testing.T) {
64-
outputAttributes := AddAddrInfoToAttributes(test.inputAddrInfo, test.inputAttributes)
65-
gotAddrInfo := GetAddrInfoFromAttributes(outputAttributes)
64+
addr := &resolver.Address{Attributes: test.inputAttributes}
65+
SetAddrInfo(test.inputAddrInfo, addr)
66+
gotAddrInfo := GetAddrInfo(addr)
6667
if !cmp.Equal(gotAddrInfo, test.wantAddrInfo) {
6768
t.Errorf("gotAddrInfo: %v, wantAddrInfo: %v", gotAddrInfo, test.wantAddrInfo)
6869
}

xds/internal/balancer/edsbalancer/eds_impl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func (edsImpl *edsBalancerImpl) handleEDSResponsePerPriority(bgwc *balancerGroup
278278
}
279279
if edsImpl.subBalancerBuilder.Name() == weightedroundrobin.Name && lbEndpoint.Weight != 0 {
280280
ai := &weightedroundrobin.AddrInfo{Weight: lbEndpoint.Weight}
281-
address.Attributes = weightedroundrobin.AddAddrInfoToAttributes(ai, address.Attributes)
281+
weightedroundrobin.SetAddrInfo(ai, &address)
282282
// Metadata field in resolver.Address is deprecated. The
283283
// attributes field should be used to specify arbitrary
284284
// attributes about the address. We still need to populate the

0 commit comments

Comments
 (0)