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

Method Close

clientconn.go:1191–1239  ·  view source on GitHub ↗

Close tears down the ClientConn and all underlying connections.

()

Source from the content-addressed store, hash-verified

1189
1190// Close tears down the ClientConn and all underlying connections.
1191func (cc *ClientConn) Close() error {
1192 defer func() {
1193 cc.cancel()
1194 <-cc.csMgr.pubSub.Done()
1195 }()
1196
1197 // Prevent calls to enter/exit idle immediately, and ensure we are not
1198 // currently entering/exiting idle mode.
1199 cc.idlenessMgr.Close()
1200
1201 cc.mu.Lock()
1202 if cc.conns == nil {
1203 cc.mu.Unlock()
1204 return ErrClientConnClosing
1205 }
1206
1207 conns := cc.conns
1208 cc.conns = nil
1209 cc.csMgr.updateState(connectivity.Shutdown)
1210
1211 // We can safely unlock and continue to access all fields now as
1212 // cc.conns==nil, preventing any further operations on cc.
1213 cc.mu.Unlock()
1214
1215 cc.resolverWrapper.close()
1216 // The order of closing matters here since the balancer wrapper assumes the
1217 // picker is closed before it is closed.
1218 cc.pickerWrapper.close()
1219 cc.balancerWrapper.close()
1220
1221 <-cc.resolverWrapper.serializer.Done()
1222 <-cc.balancerWrapper.serializer.Done()
1223 var wg sync.WaitGroup
1224 for ac := range conns {
1225 wg.Add(1)
1226 go func(ac *addrConn) {
1227 defer wg.Done()
1228 ac.tearDown(ErrClientConnClosing)
1229 }(ac)
1230 }
1231 wg.Wait()
1232 cc.addTraceEvent("deleted")
1233 // TraceEvent needs to be called before RemoveEntry, as TraceEvent may add
1234 // trace reference to the entity being deleted, and thus prevent it from being
1235 // deleted right away.
1236 channelz.RemoveEntry(cc.channelz.ID)
1237
1238 return nil
1239}
1240
1241// addrConn is a network connection to a given address.
1242type addrConn struct {

Callers 1

testLocalCredsE2ESucceedFunction · 0.95

Calls 12

addTraceEventMethod · 0.95
RemoveEntryFunction · 0.92
cancelMethod · 0.80
WaitMethod · 0.80
CloseMethod · 0.65
AddMethod · 0.65
DoneMethod · 0.45
LockMethod · 0.45
UnlockMethod · 0.45
updateStateMethod · 0.45
closeMethod · 0.45
tearDownMethod · 0.45

Tested by 1

testLocalCredsE2ESucceedFunction · 0.76