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// SetAddrInfo returns a copy of addr in which the Attributes field is updated
40// with addrInfo.
41//
42// Experimental
43//
44// Notice: This API is EXPERIMENTAL and may be changed or removed in a
45// later release.
46func SetAddrInfo(addr resolver.Address, addrInfo AddrInfo) resolver.Address {
47	addr.Attributes = addr.Attributes.WithValues(attributeKey{}, addrInfo)
48	return addr
49}
50
51// GetAddrInfo returns the AddrInfo stored in the Attributes fields of addr.
52//
53// Experimental
54//
55// Notice: This API is EXPERIMENTAL and may be changed or removed in a
56// later release.
57func GetAddrInfo(addr resolver.Address) AddrInfo {
58	v := addr.Attributes.Value(attributeKey{})
59	ai, _ := v.(AddrInfo)
60	return ai
61}
62