channelz: cleanup channel registration if Dial fails (#2733)

This commit is contained in:
lyuxuan
2019-04-02 15:42:35 -07:00
committed by GitHub
parent d389f9fac6
commit 955eb8a3c8
2 changed files with 25 additions and 13 deletions

View File

@ -134,6 +134,18 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
opt.apply(&cc.dopts)
}
defer func() {
select {
case <-ctx.Done():
conn, err = nil, ctx.Err()
default:
}
if err != nil {
cc.Close()
}
}()
if channelz.IsOn() {
if cc.dopts.channelzParentID != 0 {
cc.channelzID = channelz.RegisterChannel(&channelzChannel{cc}, cc.dopts.channelzParentID, target)
@ -196,18 +208,6 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
defer cancel()
}
defer func() {
select {
case <-ctx.Done():
conn, err = nil, ctx.Err()
default:
}
if err != nil {
cc.Close()
}
}()
scSet := false
if cc.dopts.scChan != nil {
// Try to get an initial service config.
@ -820,7 +820,7 @@ func (cc *ClientConn) Close() error {
}
channelz.AddTraceEvent(cc.channelzID, ted)
// TraceEvent needs to be called before RemoveEntry, as TraceEvent may add trace reference to
// the entity beng deleted, and thus prevent it from being deleted right away.
// the entity being deleted, and thus prevent it from being deleted right away.
channelz.RemoveEntry(cc.channelzID)
}
return nil