@@ -24,6 +24,7 @@ import (
2424 "fmt"
2525 "math"
2626 "net/url"
27+ "slices"
2728 "strings"
2829 "sync"
2930 "sync/atomic"
@@ -812,17 +813,11 @@ func (cc *ClientConn) applyFailingLBLocked(sc *serviceconfig.ParseResult) {
812813 cc .csMgr .updateState (connectivity .TransientFailure )
813814}
814815
815- // Makes a copy of the input addresses slice and clears out the balancer
816- // attributes field. Addresses are passed during subconn creation and address
817- // update operations. In both cases, we will clear the balancer attributes by
818- // calling this function, and therefore we will be able to use the Equal method
819- // provided by the resolver.Address type for comparison.
820- func copyAddressesWithoutBalancerAttributes (in []resolver.Address ) []resolver.Address {
816+ // Makes a copy of the input addresses slice. Addresses are passed during
817+ // subconn creation and address update operations.
818+ func copyAddresses (in []resolver.Address ) []resolver.Address {
821819 out := make ([]resolver.Address , len (in ))
822- for i := range in {
823- out [i ] = in [i ]
824- out [i ].BalancerAttributes = nil
825- }
820+ copy (out , in )
826821 return out
827822}
828823
@@ -837,7 +832,7 @@ func (cc *ClientConn) newAddrConnLocked(addrs []resolver.Address, opts balancer.
837832 ac := & addrConn {
838833 state : connectivity .Idle ,
839834 cc : cc ,
840- addrs : copyAddressesWithoutBalancerAttributes (addrs ),
835+ addrs : copyAddresses (addrs ),
841836 scopts : opts ,
842837 dopts : cc .dopts ,
843838 channelz : channelz .RegisterSubChannel (cc .channelz , "" ),
@@ -923,30 +918,32 @@ func (ac *addrConn) connect() error {
923918 return nil
924919}
925920
926- func equalAddresses (a , b []resolver.Address ) bool {
927- if len (a ) != len (b ) {
928- return false
929- }
930- for i , v := range a {
931- if ! v .Equal (b [i ]) {
932- return false
933- }
934- }
935- return true
921+ // equalAddressIgnoringBalAttributes returns true is a and b are considered equal.
922+ // This is different from the Equal method on the resolver.Address type which
923+ // considers all fields to determine equality. Here, we only consider fields
924+ // that are meaningful to the subConn.
925+ func equalAddressIgnoringBalAttributes (a , b * resolver.Address ) bool {
926+ return a .Addr == b .Addr && a .ServerName == b .ServerName &&
927+ a .Attributes .Equal (b .Attributes ) &&
928+ a .Metadata == b .Metadata
929+ }
930+
931+ func equalAddressesIgnoringBalAttributes (a , b []resolver.Address ) bool {
932+ return slices .EqualFunc (a , b , func (a , b resolver.Address ) bool { return equalAddressIgnoringBalAttributes (& a , & b ) })
936933}
937934
938935// updateAddrs updates ac.addrs with the new addresses list and handles active
939936// connections or connection attempts.
940937func (ac * addrConn ) updateAddrs (addrs []resolver.Address ) {
941- addrs = copyAddressesWithoutBalancerAttributes (addrs )
938+ addrs = copyAddresses (addrs )
942939 limit := len (addrs )
943940 if limit > 5 {
944941 limit = 5
945942 }
946943 channelz .Infof (logger , ac .channelz , "addrConn: updateAddrs addrs (%d of %d): %v" , limit , len (addrs ), addrs [:limit ])
947944
948945 ac .mu .Lock ()
949- if equalAddresses (ac .addrs , addrs ) {
946+ if equalAddressesIgnoringBalAttributes (ac .addrs , addrs ) {
950947 ac .mu .Unlock ()
951948 return
952949 }
@@ -965,7 +962,7 @@ func (ac *addrConn) updateAddrs(addrs []resolver.Address) {
965962 // Try to find the connected address.
966963 for _ , a := range addrs {
967964 a .ServerName = ac .cc .getServerName (a )
968- if a . Equal ( ac .curAddr ) {
965+ if equalAddressIgnoringBalAttributes ( & a , & ac .curAddr ) {
969966 // We are connected to a valid address, so do nothing but
970967 // update the addresses.
971968 ac .mu .Unlock ()
@@ -1211,7 +1208,7 @@ func (ac *addrConn) updateConnectivityState(s connectivity.State, lastErr error)
12111208 } else {
12121209 channelz .Infof (logger , ac .channelz , "Subchannel Connectivity change to %v, last error: %s" , s , lastErr )
12131210 }
1214- ac .acbw .updateState (s , lastErr )
1211+ ac .acbw .updateState (s , ac . curAddr , lastErr )
12151212}
12161213
12171214// adjustParams updates parameters used to create transports upon
0 commit comments