1/* 2 * 3 * Copyright 2019 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19// Package weightedroundrobin defines a weighted roundrobin balancer. 20package weightedroundrobin 21 22import ( 23 "google.golang.org/grpc/resolver" 24) 25 26// Name is the name of weighted_round_robin balancer. 27const Name = "weighted_round_robin" 28 29// attributeKey is the type used as the key to store AddrInfo in the Attributes 30// field of resolver.Address. 31type attributeKey struct{} 32 33// AddrInfo will be stored inside Address metadata in order to use weighted 34// roundrobin balancer. 35type AddrInfo struct { 36 Weight uint32 37} 38 39// Equal allows the values to be compared by Attributes.Equal. 40func (a AddrInfo) Equal(o interface{}) bool { 41 oa, ok := o.(AddrInfo) 42 return ok && oa.Weight == a.Weight 43} 44 45// SetAddrInfo returns a copy of addr in which the Attributes field is updated 46// with addrInfo. 47// 48// Experimental 49// 50// Notice: This API is EXPERIMENTAL and may be changed or removed in a 51// later release. 52func SetAddrInfo(addr resolver.Address, addrInfo AddrInfo) resolver.Address { 53 addr.BalancerAttributes = addr.BalancerAttributes.WithValue(attributeKey{}, addrInfo) 54 return addr 55} 56 57// GetAddrInfo returns the AddrInfo stored in the Attributes fields of addr. 58// 59// Experimental 60// 61// Notice: This API is EXPERIMENTAL and may be changed or removed in a 62// later release. 63func GetAddrInfo(addr resolver.Address) AddrInfo { 64 v := addr.BalancerAttributes.Value(attributeKey{}) 65 ai, _ := v.(AddrInfo) 66 return ai 67} 68