@@ -175,6 +175,34 @@ func (b *Balancer) clusterDiscoveryAttempt(ctx context.Context, cc *grpc.ClientC
175
175
return nil
176
176
}
177
177
178
+ func buildConnectionsState (ctx context.Context , pool interface {
179
+ GetIfPresent (endpoint endpoint.Endpoint ) conn.Conn
180
+ Allow (ctx context.Context , cc conn.Conn )
181
+ EndpointsToConnections (endpoints []endpoint.Endpoint ) []conn.Conn
182
+ }, newest []endpoint.Endpoint ,
183
+ dropped []endpoint.Endpoint ,
184
+ config balancerConfig.Config ,
185
+ selfLocation balancerConfig.Info ,
186
+ ) * connectionsState {
187
+ connections := pool .EndpointsToConnections (newest )
188
+ for _ , c := range connections {
189
+ pool .Allow (ctx , c )
190
+ c .Endpoint ().Touch ()
191
+ _ = c .Ping (ctx )
192
+ }
193
+
194
+ state := newConnectionsState (connections , config .Filter , selfLocation , config .AllowFallback )
195
+
196
+ for _ , e := range dropped {
197
+ c := pool .GetIfPresent (e )
198
+ if c != nil {
199
+ _ = c .Close (ctx )
200
+ }
201
+ }
202
+
203
+ return state
204
+ }
205
+
178
206
func (b * Balancer ) applyDiscoveredEndpoints (ctx context.Context , newest []endpoint.Endpoint , localDC string ) {
179
207
var (
180
208
onDone = trace .DriverOnBalancerUpdate (
@@ -186,10 +214,12 @@ func (b *Balancer) applyDiscoveredEndpoints(ctx context.Context, newest []endpoi
186
214
)
187
215
previous = b .connections ().All ()
188
216
)
217
+
218
+ _ , added , dropped := xslices .Diff (previous , newest , func (lhs , rhs endpoint.Endpoint ) int {
219
+ return strings .Compare (lhs .Address (), rhs .Address ())
220
+ })
221
+
189
222
defer func () {
190
- _ , added , dropped := xslices .Diff (previous , newest , func (lhs , rhs endpoint.Endpoint ) int {
191
- return strings .Compare (lhs .Address (), rhs .Address ())
192
- })
193
223
onDone (
194
224
xslices .Transform (newest , func (t endpoint.Endpoint ) trace.EndpointInfo { return t }),
195
225
xslices .Transform (added , func (t endpoint.Endpoint ) trace.EndpointInfo { return t }),
@@ -198,21 +228,27 @@ func (b *Balancer) applyDiscoveredEndpoints(ctx context.Context, newest []endpoi
198
228
)
199
229
}()
200
230
201
- connections := endpointsToConnections (b .pool , newest )
202
- for _ , c := range connections {
203
- b .pool .Allow (ctx , c )
204
- c .Endpoint ().Touch ()
205
- }
206
-
207
231
info := balancerConfig.Info {SelfLocation : localDC }
208
- state := newConnectionsState (connections , b .balancerConfig .Filter , info , b .balancerConfig .AllowFallback )
209
-
210
- endpointsInfo := make ([]endpoint.Info , len (newest ))
211
- for i , e := range newest {
212
- endpointsInfo [i ] = e
213
- }
214
-
215
- b .connectionsState .Store (state )
232
+ b .connectionsState .Store (buildConnectionsState (ctx , b .pool , newest , dropped , b .balancerConfig , info ))
233
+
234
+ //connections := b.pool.EndpointsToConnections(newest)
235
+ //for _, c := range connections {
236
+ // b.pool.Allow(ctx, c)
237
+ // c.Endpoint().Touch()
238
+ // _ = c.Ping(ctx)
239
+ //}
240
+ //
241
+ //info := balancerConfig.Info{SelfLocation: localDC}
242
+ //state := newConnectionsState(connections, b.balancerConfig.Filter, info, b.balancerConfig.AllowFallback)
243
+ //
244
+ //b.connectionsState.Store(state)
245
+ //
246
+ //for _, e := range dropped {
247
+ // c := b.pool.GetIfPresent(e)
248
+ // if c != nil {
249
+ // _ = c.Close(ctx)
250
+ // }
251
+ //}
216
252
}
217
253
218
254
func (b * Balancer ) Close (ctx context.Context ) (err error ) {
@@ -444,12 +480,3 @@ func (b *Balancer) nextConn(ctx context.Context) (c conn.Conn, err error) {
444
480
445
481
return c , nil
446
482
}
447
-
448
- func endpointsToConnections (p * conn.Pool , endpoints []endpoint.Endpoint ) []conn.Conn {
449
- conns := make ([]conn.Conn , 0 , len (endpoints ))
450
- for _ , e := range endpoints {
451
- conns = append (conns , p .Get (e ))
452
- }
453
-
454
- return conns
455
- }
0 commit comments