@@ -53,7 +53,9 @@ import (
5353
5454const (
5555 // minimum time to give a connection to complete
56- minConnectTimeout = 20 * time .Second
56+ minConnectTimeout = 20 * time .Second
57+ withBalancerAttributes = true
58+ withoutBalancerAttributes = false
5759)
5860
5961var (
@@ -812,16 +814,26 @@ func (cc *ClientConn) applyFailingLBLocked(sc *serviceconfig.ParseResult) {
812814 cc .csMgr .updateState (connectivity .TransientFailure )
813815}
814816
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 {
817+ // addressWithoutBalancerAttributes returns a copy of the input address with
818+ // the BalancerAttributes field cleared.
819+ func addressWithoutBalancerAttributes (a resolver.Address ) resolver.Address {
820+ a .BalancerAttributes = nil
821+ return a
822+ }
823+
824+ // Makes a copy of the input addresses slice and optionally clears out the
825+ // balancer attributes field. Addresses are passed during subconn creation and
826+ // address update operations. In both cases, we may clear the balancer
827+ // attributes by calling this function, which would therefore allow us to use
828+ // the Equal method provided by the resolver.Address type for comparison.
829+ func copyAddresses (in []resolver.Address , includeBalancerAttributes bool ) []resolver.Address {
821830 out := make ([]resolver.Address , len (in ))
822831 for i := range in {
823- out [i ] = in [i ]
824- out [i ].BalancerAttributes = nil
832+ if includeBalancerAttributes {
833+ out [i ] = in [i ]
834+ } else {
835+ out [i ] = addressWithoutBalancerAttributes (in [i ])
836+ }
825837 }
826838 return out
827839}
@@ -837,7 +849,7 @@ func (cc *ClientConn) newAddrConnLocked(addrs []resolver.Address, opts balancer.
837849 ac := & addrConn {
838850 state : connectivity .Idle ,
839851 cc : cc ,
840- addrs : copyAddressesWithoutBalancerAttributes (addrs ),
852+ addrs : copyAddresses (addrs , withBalancerAttributes ),
841853 scopts : opts ,
842854 dopts : cc .dopts ,
843855 channelz : channelz .RegisterSubChannel (cc .channelz , "" ),
@@ -924,12 +936,18 @@ func (ac *addrConn) connect() error {
924936 return nil
925937}
926938
927- func equalAddresses (a , b []resolver.Address ) bool {
939+ func equalAddressIgnoreBalancerAttributes (a , b resolver.Address ) bool {
940+ return a .Addr == b .Addr && a .ServerName == b .ServerName &&
941+ a .Attributes .Equal (b .Attributes ) &&
942+ a .Metadata == b .Metadata
943+ }
944+
945+ func equalAddressesIgnoreBalancerAttributes (a , b []resolver.Address ) bool {
928946 if len (a ) != len (b ) {
929947 return false
930948 }
931949 for i , v := range a {
932- if ! v . Equal ( b [i ]) {
950+ if ! equalAddressIgnoreBalancerAttributes ( v , b [i ]) {
933951 return false
934952 }
935953 }
@@ -939,15 +957,15 @@ func equalAddresses(a, b []resolver.Address) bool {
939957// updateAddrs updates ac.addrs with the new addresses list and handles active
940958// connections or connection attempts.
941959func (ac * addrConn ) updateAddrs (addrs []resolver.Address ) {
942- addrs = copyAddressesWithoutBalancerAttributes (addrs )
960+ addrs = copyAddresses (addrs , withBalancerAttributes )
943961 limit := len (addrs )
944962 if limit > 5 {
945963 limit = 5
946964 }
947- channelz .Infof (logger , ac .channelz , "addrConn: updateAddrs addrs (%d of %d): %v" , limit , len (addrs ), addrs [:limit ])
965+ channelz .Infof (logger , ac .channelz , "addrConn: updateAddrs addrs (%d of %d): %v" , limit , len (addrs ), copyAddresses ( addrs [:limit ], withoutBalancerAttributes ) )
948966
949967 ac .mu .Lock ()
950- if equalAddresses (ac .addrs , addrs ) {
968+ if equalAddressesIgnoreBalancerAttributes (ac .addrs , addrs ) {
951969 ac .mu .Unlock ()
952970 return
953971 }
@@ -966,7 +984,7 @@ func (ac *addrConn) updateAddrs(addrs []resolver.Address) {
966984 // Try to find the connected address.
967985 for _ , a := range addrs {
968986 a .ServerName = ac .cc .getServerName (a )
969- if a . Equal ( ac .curAddr ) {
987+ if equalAddressIgnoreBalancerAttributes ( a , ac .curAddr ) {
970988 // We are connected to a valid address, so do nothing but
971989 // update the addresses.
972990 ac .mu .Unlock ()
@@ -1214,7 +1232,7 @@ func (ac *addrConn) updateConnectivityState(s connectivity.State, lastErr error)
12141232 } else {
12151233 channelz .Infof (logger , ac .channelz , "Subchannel Connectivity change to %v, last error: %s" , s , lastErr )
12161234 }
1217- ac .acbw .updateState (s , lastErr )
1235+ ac .acbw .updateState (s , ac . curAddr , lastErr )
12181236}
12191237
12201238// adjustParams updates parameters used to create transports upon
@@ -1347,6 +1365,7 @@ func (ac *addrConn) tryAllAddrs(ctx context.Context, addrs []resolver.Address, c
13471365// new transport.
13481366func (ac * addrConn ) createTransport (ctx context.Context , addr resolver.Address , copts transport.ConnectOptions , connectDeadline time.Time ) error {
13491367 addr .ServerName = ac .cc .getServerName (addr )
1368+ addrWithoutBalancerAttributes := addressWithoutBalancerAttributes (addr )
13501369 hctx , hcancel := context .WithCancel (ctx )
13511370
13521371 onClose := func (r transport.GoAwayReason ) {
@@ -1381,14 +1400,14 @@ func (ac *addrConn) createTransport(ctx context.Context, addr resolver.Address,
13811400 defer cancel ()
13821401 copts .ChannelzParent = ac .channelz
13831402
1384- newTr , err := transport .NewClientTransport (connectCtx , ac .cc .ctx , addr , copts , onClose )
1403+ newTr , err := transport .NewClientTransport (connectCtx , ac .cc .ctx , addrWithoutBalancerAttributes , copts , onClose )
13851404 if err != nil {
13861405 if logger .V (2 ) {
1387- logger .Infof ("Creating new client transport to %q: %v" , addr , err )
1406+ logger .Infof ("Creating new client transport to %q: %v" , addrWithoutBalancerAttributes , err )
13881407 }
13891408 // newTr is either nil, or closed.
13901409 hcancel ()
1391- channelz .Warningf (logger , ac .channelz , "grpc: addrConn.createTransport failed to connect to %s. Err: %v" , addr , err )
1410+ channelz .Warningf (logger , ac .channelz , "grpc: addrConn.createTransport failed to connect to %s. Err: %v" , addrWithoutBalancerAttributes , err )
13921411 return err
13931412 }
13941413
0 commit comments