Skip to content

Commit

Permalink
channelz: fix missing Target on SubChannel type (#7189)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfawley authored May 3, 2024
1 parent 273fe14 commit a87e923
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions clientconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,9 @@ func (cc *ClientConn) newAddrConnLocked(addrs []resolver.Address, opts balancer.
stateChan: make(chan struct{}),
}
ac.ctx, ac.cancel = context.WithCancel(cc.ctx)
// Start with our address set to the first address; this may be updated if
// we connect to different addresses.
ac.channelz.ChannelMetrics.Target.Store(&addrs[0].Addr)

channelz.AddTraceEvent(logger, ac.channelz, 0, &channelz.TraceEvent{
Desc: "Subchannel created",
Expand Down Expand Up @@ -1304,6 +1307,7 @@ func (ac *addrConn) resetTransport() {
func (ac *addrConn) tryAllAddrs(ctx context.Context, addrs []resolver.Address, connectDeadline time.Time) error {
var firstConnErr error
for _, addr := range addrs {
ac.channelz.ChannelMetrics.Target.Store(&addr.Addr)
if ctx.Err() != nil {
return errConnClosing
}
Expand Down
4 changes: 4 additions & 0 deletions test/channelz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ func (s) TestCZGetSubChannel(t *testing.T) {
if sc == nil {
return false, fmt.Errorf("subchannel with id %v is nil", scid)
}
target := sc.ChannelMetrics.Target.Load()
if target == nil || !strings.HasPrefix(*target, "localhost") {
t.Fatalf("subchannel target must never be set incorrectly; got: %v, want <HasPrefix('localhost')>", target)
}
state := sc.ChannelMetrics.State.Load()
if state == nil || *state != connectivity.Ready {
return false, fmt.Errorf("Got subchannel state=%v; want %q", state, connectivity.Ready)
Expand Down

0 comments on commit a87e923

Please sign in to comment.