Repository: grpc/grpc-go
File: internal/resolver/delegatingresolver/delegatingresolver.go (Go)

Code before:
	r.targetResolver.Close()
	r.targetResolver = nil

	r.proxyResolver.Close()
	r.proxyResolver = nil
}

// updateClientConnStateLocked creates a list of combined addresses by
// pairing each proxy address with every target address. For each pair, it
// generates a new [resolver.Address] using the proxy address, and adding the
// target address as the attribute along with user info. It returns nil if
// either resolver has not sent update even once and returns the error from
// ClientConn update once both resolvers have sent update atleast once.
func (r *delegatingResolver) updateClientConnStateLocked() error {
	if r.targetResolverState == nil {
		return nil
	}

	curState := *r.targetResolverState
	isTCP := false

	for _, addr := range curState.Addresses {
		if networkType, ok := networktype.Get(addr); !ok || networkType == "tcp" {
			isTCP = true
			break
		}
	}

	for _, endpoint := range curState.Endpoints {
		for _, addr := range endpoint.Addresses {
			if networkType, ok := networktype.Get(addr); !ok || networkType == "tcp" || isTCP {
				isTCP = true
				break
			}
		}
	}

	// If no addresses returned by resolver have network type as tcp , do not wait for proxy update.
	if !isTCP {
		return r.cc.UpdateState(curState)
	}

	if r.proxyAddrs == nil {
		return nil
	}

	// If multiple resolved proxy addresses are present, we send only the
	// unresolved proxy host and let net.Dial handle the proxy host name
	// resolution when creating the transport. Sending all resolved addresses
	// would increase the number of addresses passed to the ClientConn and
	// subsequently to load balancing (LB) policies like Round Robin, leading

Review comment (suggestion):
Out here we're assuming `!ok` to be the same of `tcp`. However, the old code in the dialer used to parse the address string when `!ok` to determine the network type, we should probably maintain the same behaviour.
https://github.com/grpc/grpc-go/blob/172fc5bbb4618bc910e875823cf964f03f1305b1/internal/transport/http2_client.go#L177-L182

Code after:
	r.proxyResolver = nil
}

func networkTypeFromAddr(addr resolver.Address) string {
	networkType, ok := networktype.Get(addr)
	if !ok {
		networkType, _ = transport.ParseDialTarget(addr.Addr)
	}
	return networkType
}

func isTCPAddressPresent(state *resolver.State) bool {
	for _, addr := range state.Addresses {
		if networkType := networkTypeFromAddr(addr); networkType == "tcp" {
			return true
		}
	}
	for _, endpoint := range state.Endpoints {
		for _, addr := range endpoint.Addresses {
			if networktype := networkTypeFromAddr(addr); networktype == "tcp" {
				return true
			}
		}
	}
	return false
}

// updateClientConnStateLocked constructs a combined list of addresses by
// pairing each proxy address with every target address of type TCP. For each
// pair, it creates a new [resolver.Address] using the proxy address and
// attaches the corresponding target address and user info as attributes. Target
// addresses that are not of type TCP are appended to the list as-is. The
// function returns nil if either resolver has not yet provided an update, and
// returns the result of ClientConn.UpdateState once both resolvers have
// provided at least one update.
func (r *delegatingResolver) updateClientConnStateLocked() error {
	if r.targetResolverState == nil || r.proxyAddrs == nil {
		return nil
	}

	// If multiple resolved proxy addresses are present, we send only the
	// unresolved proxy host and let net.Dial handle the proxy host name
	// resolution when creating the transport. Sending all resolved addresses
	// would increase the number of addresses passed to the ClientConn and
	// subsequently to load balancing (LB) policies like Round Robin, leading
	// to additional TCP connections. However, if there's only one resolved
	// proxy address, we send it directly, as it doesn't affect the address
	// count returned by the target resolver and the address count sent to the
	// ClientConn.
	var proxyAddr resolver.Address
	if len(r.proxyAddrs) == 1 {