blob: 22045bf3946c3a0f7b946cd439cf4ba25aed2a54 [file] [log] [blame]
khenaidooac637102019-01-14 15:44:34 -05001/*
2 *
3 * Copyright 2017 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 roundrobin defines a roundrobin balancer. Roundrobin balancer is
20// installed as one of the default balancers in gRPC, users don't need to
21// explicitly install this balancer.
22package roundrobin
23
24import (
Abhay Kumara2ae5992025-11-10 14:02:24 +000025 "fmt"
khenaidooac637102019-01-14 15:44:34 -050026
27 "google.golang.org/grpc/balancer"
Abhay Kumara2ae5992025-11-10 14:02:24 +000028 "google.golang.org/grpc/balancer/endpointsharding"
29 "google.golang.org/grpc/balancer/pickfirst/pickfirstleaf"
khenaidooac637102019-01-14 15:44:34 -050030 "google.golang.org/grpc/grpclog"
Abhay Kumara2ae5992025-11-10 14:02:24 +000031 internalgrpclog "google.golang.org/grpc/internal/grpclog"
khenaidooac637102019-01-14 15:44:34 -050032)
33
34// Name is the name of round_robin balancer.
35const Name = "round_robin"
36
Abhay Kumara2ae5992025-11-10 14:02:24 +000037var logger = grpclog.Component("roundrobin")
khenaidooac637102019-01-14 15:44:34 -050038
39func init() {
Abhay Kumara2ae5992025-11-10 14:02:24 +000040 balancer.Register(builder{})
khenaidooac637102019-01-14 15:44:34 -050041}
42
Abhay Kumara2ae5992025-11-10 14:02:24 +000043type builder struct{}
khenaidooac637102019-01-14 15:44:34 -050044
Abhay Kumara2ae5992025-11-10 14:02:24 +000045func (bb builder) Name() string {
46 return Name
khenaidooac637102019-01-14 15:44:34 -050047}
48
Abhay Kumara2ae5992025-11-10 14:02:24 +000049func (bb builder) Build(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer {
50 childBuilder := balancer.Get(pickfirstleaf.Name).Build
51 bal := &rrBalancer{
52 cc: cc,
53 Balancer: endpointsharding.NewBalancer(cc, opts, childBuilder, endpointsharding.Options{}),
54 }
55 bal.logger = internalgrpclog.NewPrefixLogger(logger, fmt.Sprintf("[%p] ", bal))
56 bal.logger.Infof("Created")
57 return bal
khenaidooac637102019-01-14 15:44:34 -050058}
59
Abhay Kumara2ae5992025-11-10 14:02:24 +000060type rrBalancer struct {
61 balancer.Balancer
62 cc balancer.ClientConn
63 logger *internalgrpclog.PrefixLogger
64}
65
66func (b *rrBalancer) UpdateClientConnState(ccs balancer.ClientConnState) error {
67 return b.Balancer.UpdateClientConnState(balancer.ClientConnState{
68 // Enable the health listener in pickfirst children for client side health
69 // checks and outlier detection, if configured.
70 ResolverState: pickfirstleaf.EnableHealthListener(ccs.ResolverState),
71 })
khenaidooac637102019-01-14 15:44:34 -050072}