Close tears down the ClientConn and all underlying connections.
()
| 1189 | |
| 1190 | // Close tears down the ClientConn and all underlying connections. |
| 1191 | func (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. |
| 1242 | type addrConn struct { |