@@ -499,13 +499,7 @@ type ClientConn struct {
499
499
// connections accordingly. If doneChan is not nil, it is closed after the
500
500
// first successfull connection is made.
501
501
func (cc * ClientConn ) lbWatcher (doneChan chan struct {}) {
502
- var (
503
- firstFindConn * addrConn // tear down when the cc.conns is empty
504
- isPickFirst bool // true: roundrobin, false: pickfirst
505
- )
506
- if reflect .TypeOf (cc .dopts .balancer ) == reflect .TypeOf (& pickFirst {}) {
507
- isPickFirst = true
508
- }
502
+ isPickFirst := reflect .TypeOf (cc .dopts .balancer ) == reflect .TypeOf (& pickFirst {})
509
503
for addrs := range cc .dopts .balancer .Notify () {
510
504
var (
511
505
add []Address // Addresses need to setup connections.
@@ -515,12 +509,9 @@ func (cc *ClientConn) lbWatcher(doneChan chan struct{}) {
515
509
for _ , addr := range addrs {
516
510
if _ , ok := cc .conns [addr ]; ! ok {
517
511
add = append (add , addr )
518
- } else {
519
- firstFindConn = cc .conns [addr ]
520
512
}
521
513
}
522
514
for k , c := range cc .conns {
523
- firstFindConn = c
524
515
var keep bool
525
516
for _ , a := range addrs {
526
517
if k == a {
@@ -554,20 +545,11 @@ func (cc *ClientConn) lbWatcher(doneChan chan struct{}) {
554
545
}
555
546
}
556
547
}
557
-
558
- if isPickFirst {
559
- // Notify may delete all address. time to tear down, or leave it?
560
- if len (cc .conns ) == 0 {
561
- if firstFindConn != nil {
562
- firstFindConn .tearDown (errConnDrain )
563
- }
564
- }
565
- } else {
548
+ if ! isPickFirst {
566
549
for _ , c := range del {
567
550
c .tearDown (errConnDrain )
568
551
}
569
552
}
570
-
571
553
}
572
554
}
573
555
@@ -599,6 +581,7 @@ func (cc *ClientConn) scWatcher() {
599
581
func (cc * ClientConn ) resetAddrConn (addrs []Address , block bool , tearDownErr error ) error {
600
582
// if current transport in addrs, just change lists to update order and new addresses
601
583
// not work for roundrobin
584
+ cc .mu .Lock ()
602
585
if len (cc .conns ) != 0 && (reflect .TypeOf (cc .dopts .balancer ) == reflect .TypeOf (& pickFirst {})) {
603
586
var currentAc * addrConn
604
587
for _ , v := range cc .conns {
@@ -607,7 +590,7 @@ func (cc *ClientConn) resetAddrConn(addrs []Address, block bool, tearDownErr err
607
590
}
608
591
var addrInUse bool
609
592
for _ , addr := range addrs {
610
- if addr == currentAc .curAddr {
593
+ if strings . Compare ( addr . Addr , currentAc .curAddr . Addr ) == 0 {
611
594
addrInUse = true
612
595
break
613
596
}
@@ -618,9 +601,11 @@ func (cc *ClientConn) resetAddrConn(addrs []Address, block bool, tearDownErr err
618
601
cc .conns [addr ] = currentAc
619
602
}
620
603
currentAc .addrs = addrs
604
+ cc .mu .Unlock ()
621
605
return nil
622
606
}
623
607
}
608
+ cc .mu .Unlock ()
624
609
625
610
ac := & addrConn {
626
611
cc : cc ,
@@ -887,7 +872,7 @@ func (ac *addrConn) waitForStateChange(ctx context.Context, sourceState Connecti
887
872
return ac .state , nil
888
873
}
889
874
890
- // resetTransport recreates a transport to the address for ac.
875
+ /// resetTransport recreates a transport to the address for ac.
891
876
// For the old transport:
892
877
// - if drain is true, it will be gracefully closed.
893
878
// - otherwise, it will be closed.
@@ -947,14 +932,13 @@ func (ac *addrConn) resetTransport(drain bool) error {
947
932
cancel ()
948
933
949
934
if e , ok := err .(transport.ConnectionError ); ok && ! e .Temporary () {
950
- ac .curAddr .Addr = ""
935
+ // ac.curAddr.Addr = ""
951
936
return err
952
937
}
953
938
grpclog .Warningf ("grpc: addrConn.resetTransport failed to create client transport: %v; Reconnecting to %v" , err , addr )
954
939
ac .mu .Lock ()
955
940
if ac .state == Shutdown {
956
941
// ac.tearDown(...) has been invoked.
957
- ac .curAddr .Addr = ""
958
942
ac .mu .Unlock ()
959
943
return errConnClosing
960
944
}
@@ -977,7 +961,7 @@ func (ac *addrConn) resetTransport(drain bool) error {
977
961
ac .printf ("ready" )
978
962
if ac .state == Shutdown {
979
963
// ac.tearDown(...) has been invoked.
980
- ac .curAddr .Addr = ""
964
+ // ac.curAddr.Addr = ""
981
965
ac .mu .Unlock ()
982
966
newTransport .Close ()
983
967
return errConnClosing
0 commit comments