|
19 | 19 | // Package weightedroundrobin defines a weighted roundrobin balancer. |
20 | 20 | package weightedroundrobin |
21 | 21 |
|
22 | | -import "google.golang.org/grpc/attributes" |
| 22 | +import ( |
| 23 | + "google.golang.org/grpc/resolver" |
| 24 | +) |
23 | 25 |
|
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" |
27 | 28 |
|
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{} |
32 | 32 |
|
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. |
35 | 35 | type AddrInfo struct { |
36 | 36 | Weight uint32 |
37 | 37 | } |
38 | 38 |
|
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) |
43 | 42 | } |
44 | 43 |
|
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 { |
49 | 48 | return nil |
50 | 49 | } |
51 | | - ai := a.Value(attributeKey) |
| 50 | + ai := addr.Attributes.Value(attributeKey{}) |
52 | 51 | if ai == nil { |
53 | 52 | return nil |
54 | 53 | } |
|
0 commit comments