@@ -27,21 +27,16 @@ package endpointsharding
2727
2828import (
2929 "errors"
30- "fmt"
3130 rand "math/rand/v2"
3231 "sync"
3332 "sync/atomic"
3433
3534 "google.golang.org/grpc/balancer"
3635 "google.golang.org/grpc/balancer/base"
3736 "google.golang.org/grpc/connectivity"
38- "google.golang.org/grpc/grpclog"
39- internalgrpclog "google.golang.org/grpc/internal/grpclog"
4037 "google.golang.org/grpc/resolver"
4138)
4239
43- var logger = grpclog .Component ("endpointsharding-lb" )
44-
4540// ChildState is the balancer state of a child along with the endpoint which
4641// identifies the child balancer.
4742type ChildState struct {
@@ -50,7 +45,15 @@ type ChildState struct {
5045
5146 // Balancer exposes only the ExitIdler interface of the child LB policy.
5247 // Other methods of the child policy are called only by endpointsharding.
53- Balancer balancer.ExitIdler
48+ Balancer ExitIdler
49+ }
50+
51+ // ExitIdler provides access to only the ExitIdle method of the child balancer.
52+ type ExitIdler interface {
53+ // ExitIdle instructs the LB policy to reconnect to backends / exit the
54+ // IDLE state, if appropriate and possible. Note that SubConns that enter
55+ // the IDLE state will not reconnect until SubConn.Connect is called.
56+ ExitIdle ()
5457}
5558
5659// Options are the options to configure the behaviour of the
@@ -78,7 +81,6 @@ func NewBalancer(cc balancer.ClientConn, opts balancer.BuildOptions, childBuilde
7881 esOpts : esOpts ,
7982 childBuilder : childBuilder ,
8083 }
81- es .logger = internalgrpclog .NewPrefixLogger (logger , fmt .Sprintf ("[endpointsharding-lb %p] " , es ))
8284 es .children .Store (resolver .NewEndpointMap [* balancerWrapper ]())
8385 return es
8486}
@@ -91,7 +93,6 @@ type endpointSharding struct {
9193 bOpts balancer.BuildOptions
9294 esOpts Options
9395 childBuilder ChildBuilderFunc
94- logger * internalgrpclog.PrefixLogger // Prefix logger for all logging.
9596
9697 // childMu synchronizes calls to any single child. It must be held for all
9798 // calls into a child. To avoid deadlocks, do not acquire childMu while
@@ -220,11 +221,8 @@ func (es *endpointSharding) ExitIdle() {
220221 // exitidle (but still checks for the interface's existence to
221222 // avoid a panic if not). If the child does not, no subconns
222223 // will be connected.
223- ei , ok := bw .child .(balancer.ExitIdler )
224- if ok && ! bw .isClosed {
225- ei .ExitIdle ()
226- } else if ! ok {
227- es .logger .Errorf ("Child balancer doesn't implement ExitIdler." )
224+ if ! bw .isClosed {
225+ bw .child .ExitIdle ()
228226 }
229227 }
230228}
@@ -350,21 +348,13 @@ func (bw *balancerWrapper) UpdateState(state balancer.State) {
350348// ExitIdle pings an IDLE child balancer to exit idle in a new goroutine to
351349// avoid deadlocks due to synchronous balancer state updates.
352350func (bw * balancerWrapper ) ExitIdle () {
353- // this implementation assumes the child balancer supports
354- // exitidle (but still checks for the interface's existence to
355- // avoid a panic if not). If the child does not, no subconns
356- // will be connected.
357- if ei , ok := bw .child .(balancer.ExitIdler ); ok {
358- go func () {
359- bw .es .childMu .Lock ()
360- if ! bw .isClosed {
361- ei .ExitIdle ()
362- }
363- bw .es .childMu .Unlock ()
364- }()
365- } else if ! ok {
366- bw .es .logger .Errorf ("Child balancer doesn't implement ExitIdler." )
367- }
351+ go func () {
352+ bw .es .childMu .Lock ()
353+ if ! bw .isClosed {
354+ bw .child .ExitIdle ()
355+ }
356+ bw .es .childMu .Unlock ()
357+ }()
368358}
369359
370360// updateClientConnStateLocked delivers the ClientConnState to the child
0 commit comments