Skip to content

Commit 3e994fa

Browse files
committed
balancer: expose parent ClientConn via Unwrap() on all embedding wrappers
Several types in the balancer tree wrap a balancer.ClientConn by anonymously embedding it. Add an Unwrap() balancer.ClientConn method to each of them so that gRPC-internal callers can walk up the wrapping chain to reach the underlying parent ClientConn. This is the mirror-image contract of what errors.Unwrap provides for error trees. The method is a one-liner returning the embedded field, and doesn't change any existing behavior. It exists so a follow-up change can rely on it to inherit parent-channel telemetry (stats handlers and interceptors) onto balancer-owned control channels (e.g. the RLS control channel) without resorting to reflection over private fields. Wrappers touched: internal/balancer/gracefulswitch.balancerWrapper internal/balancergroup.subBalancerWrapper internal/xds/balancer/clusterimpl.clusterImplBalancer internal/xds/balancer/outlierdetection.outlierDetectionBalancer internal/xds/balancer/priority.ignoreResolveNowClientConn balancer/grpclb.lbCacheClientConn balancer/ringhash.ringhashBalancer RELEASE NOTES: none
1 parent 9d1988d commit 3e994fa

7 files changed

Lines changed: 49 additions & 0 deletions

File tree

balancer/grpclb/grpclb_util.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ func (ccc *lbCacheClientConn) RemoveSubConn(sc balancer.SubConn) {
9191
logger.Errorf("RemoveSubConn(%v) called unexpectedly", sc)
9292
}
9393

94+
// Unwrap returns the ClientConn that this wrapper delegates to. It lets
95+
// gRPC-internal callers walk past the grpclb subconn-cache layer to reach the
96+
// underlying parent ClientConn.
97+
func (ccc *lbCacheClientConn) Unwrap() balancer.ClientConn {
98+
return ccc.ClientConn
99+
}
100+
94101
type lbCacheSubConn struct {
95102
balancer.SubConn
96103
ccc *lbCacheClientConn

balancer/ringhash/ringhash.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ func (b *ringhashBalancer) UpdateState(state balancer.State) {
178178
b.updatePickerLocked()
179179
}
180180

181+
// Unwrap returns the ClientConn that this wrapper delegates to. It lets
182+
// gRPC-internal callers walk past the ring-hash layer to reach the underlying
183+
// parent ClientConn.
184+
func (b *ringhashBalancer) Unwrap() balancer.ClientConn {
185+
return b.ClientConn
186+
}
187+
181188
func (b *ringhashBalancer) UpdateClientConnState(ccs balancer.ClientConnState) error {
182189
if b.logger.V(2) {
183190
b.logger.Infof("Received update from resolver, balancer config: %+v", pretty.ToJSON(ccs.BalancerConfig))

internal/balancer/gracefulswitch/gracefulswitch.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,13 @@ type balancerWrapper struct {
302302
subconns map[balancer.SubConn]bool // subconns created by this balancer
303303
}
304304

305+
// Unwrap returns the ClientConn that this wrapper delegates to. It lets
306+
// gRPC-internal callers walk past the graceful-switch layer to reach the
307+
// underlying parent ClientConn.
308+
func (bw *balancerWrapper) Unwrap() balancer.ClientConn {
309+
return bw.ClientConn
310+
}
311+
305312
// Close closes the underlying LB policy and shuts down the subconns it
306313
// created. bw must not be referenced via balancerCurrent or balancerPending in
307314
// gsb when called. gsb.mu must not be held. Does not panic with a nil

internal/balancergroup/balancergroup.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ type subBalancerWrapper struct {
7373
balancer *gracefulswitch.Balancer
7474
}
7575

76+
// Unwrap returns the ClientConn that this wrapper delegates to. It lets
77+
// gRPC-internal callers walk past the balancer-group layer to reach the
78+
// underlying parent ClientConn.
79+
func (sbc *subBalancerWrapper) Unwrap() balancer.ClientConn {
80+
return sbc.ClientConn
81+
}
82+
7683
// UpdateState overrides balancer.ClientConn, to keep state and picker.
7784
func (sbc *subBalancerWrapper) UpdateState(state balancer.State) {
7885
sbc.mu.Lock()

internal/xds/balancer/clusterimpl/clusterimpl.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,13 @@ func (b *clusterImplBalancer) handleSecurityConfig(config *xdsresource.SecurityC
390390
return nil
391391
}
392392

393+
// Unwrap returns the ClientConn that this wrapper delegates to. It lets
394+
// gRPC-internal callers walk past the cluster_impl layer to reach the
395+
// underlying parent ClientConn.
396+
func (b *clusterImplBalancer) Unwrap() balancer.ClientConn {
397+
return b.ClientConn
398+
}
399+
393400
func (b *clusterImplBalancer) UpdateClientConnState(s balancer.ClientConnState) error {
394401
defer clientConnUpdateHook()
395402

internal/xds/balancer/outlierdetection/balancer.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,13 @@ type outlierDetectionBalancer struct {
233233
pickerUpdateCh *buffer.Unbounded[any]
234234
}
235235

236+
// Unwrap returns the ClientConn that this wrapper delegates to. It lets
237+
// gRPC-internal callers walk past the outlier-detection layer to reach the
238+
// underlying parent ClientConn.
239+
func (b *outlierDetectionBalancer) Unwrap() balancer.ClientConn {
240+
return b.ClientConn
241+
}
242+
236243
// noopConfig returns whether this balancer is configured with a logical no-op
237244
// configuration or not.
238245
//

internal/xds/balancer/priority/ignore_resolve_now.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,10 @@ func (i *ignoreResolveNowClientConn) ResolveNow(o resolver.ResolveNowOptions) {
4848
}
4949
i.ClientConn.ResolveNow(o)
5050
}
51+
52+
// Unwrap returns the ClientConn that this wrapper delegates to. It lets
53+
// gRPC-internal callers walk past the priority layer to reach the underlying
54+
// parent ClientConn.
55+
func (i *ignoreResolveNowClientConn) Unwrap() balancer.ClientConn {
56+
return i.ClientConn
57+
}

0 commit comments

Comments
 (0)