From ad81c20503be8c36d929741078e1a53a292e4048 Mon Sep 17 00:00:00 2001 From: Easwar Swaminathan Date: Mon, 14 Oct 2024 07:57:45 -0700 Subject: [PATCH] pickfirstleaf: minor simplification to reconcileSubConnsLocked method (#7731) --- .../pickfirst/pickfirstleaf/pickfirstleaf.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/balancer/pickfirst/pickfirstleaf/pickfirstleaf.go b/balancer/pickfirst/pickfirstleaf/pickfirstleaf.go index 48ce8c50e5c1..985b6edc7f4c 100644 --- a/balancer/pickfirst/pickfirstleaf/pickfirstleaf.go +++ b/balancer/pickfirst/pickfirstleaf/pickfirstleaf.go @@ -314,21 +314,22 @@ func deDupAddresses(addrs []resolver.Address) []resolver.Address { return retAddrs } +// reconcileSubConnsLocked updates the active subchannels based on a new address +// list from the resolver. It does this by: +// - closing subchannels: any existing subchannels associated with addresses +// that are no longer in the updated list are shut down. +// - removing subchannels: entries for these closed subchannels are removed +// from the subchannel map. +// +// This ensures that the subchannel map accurately reflects the current set of +// addresses received from the name resolver. func (b *pickfirstBalancer) reconcileSubConnsLocked(newAddrs []resolver.Address) { - // Remove old subConns that were not in new address list. - oldAddrsMap := resolver.NewAddressMap() - for _, k := range b.subConns.Keys() { - oldAddrsMap.Set(k, true) - } - - // Flatten the new endpoint addresses. newAddrsMap := resolver.NewAddressMap() for _, addr := range newAddrs { newAddrsMap.Set(addr, true) } - // Shut them down and remove them. - for _, oldAddr := range oldAddrsMap.Keys() { + for _, oldAddr := range b.subConns.Keys() { if _, ok := newAddrsMap.Get(oldAddr); ok { continue }