MCPcopy
hub / github.com/grpc/grpc-go / UpdateClientConnState

Method UpdateClientConnState

balancer/base/balancer.go:95–145  ·  view source on GitHub ↗
(s balancer.ClientConnState)

Source from the content-addressed store, hash-verified

93}
94
95func (b *baseBalancer) UpdateClientConnState(s balancer.ClientConnState) error {
96 // TODO: handle s.ResolverState.ServiceConfig?
97 if logger.V(2) {
98 logger.Info("base.baseBalancer: got new ClientConn state: ", s)
99 }
100 // Successful resolution; clear resolver error and ensure we return nil.
101 b.resolverErr = nil
102 // addrsSet is the set converted from addrs, it's used for quick lookup of an address.
103 addrsSet := resolver.NewAddressMapV2[any]()
104 for _, a := range s.ResolverState.Addresses {
105 addrsSet.Set(a, nil)
106 if _, ok := b.subConns.Get(a); !ok {
107 // a is a new address (not existing in b.subConns).
108 var sc balancer.SubConn
109 opts := balancer.NewSubConnOptions{
110 HealthCheckEnabled: b.config.HealthCheck,
111 StateListener: func(scs balancer.SubConnState) { b.updateSubConnState(sc, scs) },
112 }
113 sc, err := b.cc.NewSubConn([]resolver.Address{a}, opts)
114 if err != nil {
115 logger.Warningf("base.baseBalancer: failed to create new SubConn: %v", err)
116 continue
117 }
118 b.subConns.Set(a, sc)
119 b.scStates[sc] = connectivity.Idle
120 b.csEvltr.RecordTransition(connectivity.Shutdown, connectivity.Idle)
121 sc.Connect()
122 }
123 }
124 for a, sc := range b.subConns.All() {
125 // a was removed by resolver.
126 if _, ok := addrsSet.Get(a); !ok {
127 sc.Shutdown()
128 b.subConns.Delete(a)
129 // Keep the state of this sc in b.scStates until sc's state becomes Shutdown.
130 // The entry will be deleted in updateSubConnState.
131 }
132 }
133 // If resolver state contains no addresses, return an error so ClientConn
134 // will trigger re-resolve. Also records this as a resolver error, so when
135 // the overall state turns transient failure, the error message will have
136 // the zero address information.
137 if len(s.ResolverState.Addresses) == 0 {
138 b.ResolverError(errors.New("produced zero addresses"))
139 return balancer.ErrBadResolverState
140 }
141
142 b.regeneratePicker()
143 b.cc.UpdateState(balancer.State{ConnectivityState: b.state, Picker: b.picker})
144 return nil
145}
146
147// mergeErrors builds an error from the last connection error and the last
148// resolver error. Must only be called if b.state is TransientFailure.

Callers

nothing calls this directly

Calls 15

updateSubConnStateMethod · 0.95
ConnectMethod · 0.95
ShutdownMethod · 0.95
ResolverErrorMethod · 0.95
regeneratePickerMethod · 0.95
RecordTransitionMethod · 0.80
VMethod · 0.65
InfoMethod · 0.65
SetMethod · 0.65
GetMethod · 0.65
NewSubConnMethod · 0.65
WarningfMethod · 0.65

Tested by

no test coverage detected