@@ -28,19 +28,33 @@ package endpointsharding
2828import (
2929 "encoding/json"
3030 "errors"
31+ "fmt"
32+ rand "math/rand/v2"
3133 "sync"
3234 "sync/atomic"
3335
34- rand "math/rand/v2"
35-
3636 "google.golang.org/grpc/balancer"
3737 "google.golang.org/grpc/balancer/base"
38+ "google.golang.org/grpc/balancer/pickfirst"
39+ "google.golang.org/grpc/balancer/pickfirst/pickfirstleaf"
3840 "google.golang.org/grpc/connectivity"
3941 "google.golang.org/grpc/internal/balancer/gracefulswitch"
42+ "google.golang.org/grpc/internal/envconfig"
4043 "google.golang.org/grpc/resolver"
4144 "google.golang.org/grpc/serviceconfig"
4245)
4346
47+ // PickFirstConfig is a pick first config without shuffling enabled.
48+ var PickFirstConfig string
49+
50+ func init () {
51+ name := pickfirst .Name
52+ if ! envconfig .NewPickFirstEnabled {
53+ name = pickfirstleaf .Name
54+ }
55+ PickFirstConfig = fmt .Sprintf ("[{%q: {}}]" , name )
56+ }
57+
4458// ChildState is the balancer state of a child along with the endpoint which
4559// identifies the child balancer.
4660type ChildState struct {
@@ -100,9 +114,6 @@ func (es *endpointSharding) UpdateClientConnState(state balancer.ClientConnState
100114
101115 // Update/Create new children.
102116 for _ , endpoint := range state .ResolverState .Endpoints {
103- if len (endpoint .Addresses ) == 0 {
104- continue
105- }
106117 if _ , ok := newChildren .Get (endpoint ); ok {
107118 // Endpoint child was already created, continue to avoid duplicate
108119 // update.
@@ -143,6 +154,9 @@ func (es *endpointSharding) UpdateClientConnState(state balancer.ClientConnState
143154 }
144155 }
145156 es .children .Store (newChildren )
157+ if newChildren .Len () == 0 {
158+ return balancer .ErrBadResolverState
159+ }
146160 return ret
147161}
148162
@@ -306,6 +320,3 @@ func (bw *balancerWrapper) UpdateState(state balancer.State) {
306320func ParseConfig (cfg json.RawMessage ) (serviceconfig.LoadBalancingConfig , error ) {
307321 return gracefulswitch .ParseConfig (cfg )
308322}
309-
310- // PickFirstConfig is a pick first config without shuffling enabled.
311- const PickFirstConfig = "[{\" pick_first\" : {}}]"
0 commit comments