@@ -20,14 +20,14 @@ package grpc
20
20
21
21
import (
22
22
"errors"
23
- "fmt"
24
23
"net"
25
24
"strings"
26
25
"sync"
27
26
"time"
28
27
29
28
"golang.org/x/net/context"
30
29
"golang.org/x/net/trace"
30
+ "google.golang.org/grpc/connectivity"
31
31
"google.golang.org/grpc/credentials"
32
32
"google.golang.org/grpc/grpclog"
33
33
"google.golang.org/grpc/keepalive"
@@ -445,39 +445,6 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
445
445
return cc , nil
446
446
}
447
447
448
- // ConnectivityState indicates the state of a client connection.
449
- type ConnectivityState int
450
-
451
- const (
452
- // Idle indicates the ClientConn is idle.
453
- Idle ConnectivityState = iota
454
- // Connecting indicates the ClienConn is connecting.
455
- Connecting
456
- // Ready indicates the ClientConn is ready for work.
457
- Ready
458
- // TransientFailure indicates the ClientConn has seen a failure but expects to recover.
459
- TransientFailure
460
- // Shutdown indicates the ClientConn has started shutting down.
461
- Shutdown
462
- )
463
-
464
- func (s ConnectivityState ) String () string {
465
- switch s {
466
- case Idle :
467
- return "IDLE"
468
- case Connecting :
469
- return "CONNECTING"
470
- case Ready :
471
- return "READY"
472
- case TransientFailure :
473
- return "TRANSIENT_FAILURE"
474
- case Shutdown :
475
- return "SHUTDOWN"
476
- default :
477
- panic (fmt .Sprintf ("unknown connectivity state: %d" , s ))
478
- }
479
- }
480
-
481
448
// connectivityStateEvaluator gets updated by addrConns when their
482
449
// states transition, based on which it evaluates the state of
483
450
// ClientConn.
@@ -492,55 +459,55 @@ type connectivityStateEvaluator struct {
492
459
493
460
// recordTransition records state change happening in every addrConn and based on
494
461
// that it evaluates what state the ClientConn is in.
495
- // It can only transition between Ready, Connecting and TransientFailure. Other states,
496
- // Idle and Shutdown are transitioned into by ClientConn; in the begining of the connection
462
+ // It can only transition between connectivity. Ready, connectivity. Connecting and connectivity. TransientFailure. Other states,
463
+ // Idle and connectivity. Shutdown are transitioned into by ClientConn; in the begining of the connection
497
464
// before any addrConn is created ClientConn is in idle state. In the end when ClientConn
498
- // closes it is in Shutdown state.
465
+ // closes it is in connectivity. Shutdown state.
499
466
// TODO Note that in later releases, a ClientConn with no activity will be put into an Idle state.
500
- func (cse * connectivityStateEvaluator ) recordTransition (oldState , newState ConnectivityState ) {
467
+ func (cse * connectivityStateEvaluator ) recordTransition (oldState , newState connectivity. State ) {
501
468
cse .mu .Lock ()
502
469
defer cse .mu .Unlock ()
503
470
504
471
// Update counters.
505
- for idx , state := range []ConnectivityState {oldState , newState } {
472
+ for idx , state := range []connectivity. State {oldState , newState } {
506
473
updateVal := 2 * uint64 (idx ) - 1 // -1 for oldState and +1 for new.
507
474
switch state {
508
- case Ready :
475
+ case connectivity . Ready :
509
476
cse .numReady += updateVal
510
- case Connecting :
477
+ case connectivity . Connecting :
511
478
cse .numConnecting += updateVal
512
- case TransientFailure :
479
+ case connectivity . TransientFailure :
513
480
cse .numTransientFailure += updateVal
514
481
}
515
482
}
516
483
517
484
// Evaluate.
518
485
if cse .numReady > 0 {
519
- cse .csMgr .updateState (Ready )
486
+ cse .csMgr .updateState (connectivity . Ready )
520
487
return
521
488
}
522
489
if cse .numConnecting > 0 {
523
- cse .csMgr .updateState (Connecting )
490
+ cse .csMgr .updateState (connectivity . Connecting )
524
491
return
525
492
}
526
- cse .csMgr .updateState (TransientFailure )
493
+ cse .csMgr .updateState (connectivity . TransientFailure )
527
494
}
528
495
529
- // connectivityStateManager keeps the ConnectivityState of ClientConn.
496
+ // connectivityStateManager keeps the connectivity.State of ClientConn.
530
497
// This struct will eventually be exported so the balancers can access it.
531
498
type connectivityStateManager struct {
532
499
mu sync.Mutex
533
- state ConnectivityState
500
+ state connectivity. State
534
501
notifyChan chan struct {}
535
502
}
536
503
537
- // updateState updates the ConnectivityState of ClientConn.
504
+ // updateState updates the connectivity.State of ClientConn.
538
505
// If there's a change it notifies goroutines waiting on state change to
539
506
// happen.
540
- func (csm * connectivityStateManager ) updateState (state ConnectivityState ) {
507
+ func (csm * connectivityStateManager ) updateState (state connectivity. State ) {
541
508
csm .mu .Lock ()
542
509
defer csm .mu .Unlock ()
543
- if csm .state == Shutdown {
510
+ if csm .state == connectivity . Shutdown {
544
511
return
545
512
}
546
513
if csm .state == state {
@@ -554,7 +521,7 @@ func (csm *connectivityStateManager) updateState(state ConnectivityState) {
554
521
}
555
522
}
556
523
557
- func (csm * connectivityStateManager ) getState () ConnectivityState {
524
+ func (csm * connectivityStateManager ) getState () connectivity. State {
558
525
csm .mu .Lock ()
559
526
defer csm .mu .Unlock ()
560
527
return csm .state
@@ -587,9 +554,10 @@ type ClientConn struct {
587
554
mkp keepalive.ClientParameters
588
555
}
589
556
590
- // WaitForStateChange waits until the ConnectivityState of ClientConn changes from sourceState or
557
+ // WaitForStateChange waits until the connectivity.State of ClientConn changes from sourceState or
591
558
// ctx expires. A true value is returned in former case and false in latter.
592
- func (cc * ClientConn ) WaitForStateChange (ctx context.Context , sourceState ConnectivityState ) bool {
559
+ // This is an EXPERIMENTAL API.
560
+ func (cc * ClientConn ) WaitForStateChange (ctx context.Context , sourceState connectivity.State ) bool {
593
561
ch := cc .csMgr .getNotifyChan ()
594
562
if cc .csMgr .getState () != sourceState {
595
563
return true
@@ -602,8 +570,9 @@ func (cc *ClientConn) WaitForStateChange(ctx context.Context, sourceState Connec
602
570
}
603
571
}
604
572
605
- // GetState returns the ConnectivityState of ClientConn.
606
- func (cc * ClientConn ) GetState () ConnectivityState {
573
+ // GetState returns the connectivity.State of ClientConn.
574
+ // This is an EXPERIMENTAL API.
575
+ func (cc * ClientConn ) GetState () connectivity.State {
607
576
return cc .csMgr .getState ()
608
577
}
609
578
@@ -855,7 +824,7 @@ func (cc *ClientConn) Close() error {
855
824
}
856
825
conns := cc .conns
857
826
cc .conns = nil
858
- cc .csMgr .updateState (Shutdown )
827
+ cc .csMgr .updateState (connectivity . Shutdown )
859
828
cc .mu .Unlock ()
860
829
if cc .dopts .balancer != nil {
861
830
cc .dopts .balancer .Close ()
@@ -879,7 +848,7 @@ type addrConn struct {
879
848
csEvltr * connectivityStateEvaluator
880
849
881
850
mu sync.Mutex
882
- state ConnectivityState
851
+ state connectivity. State
883
852
down func (error ) // the handler called when a connection is down.
884
853
// ready is closed and becomes nil when a new transport is up or failed
885
854
// due to timeout.
@@ -926,7 +895,7 @@ func (ac *addrConn) errorf(format string, a ...interface{}) {
926
895
// - otherwise, it will be closed.
927
896
func (ac * addrConn ) resetTransport (drain bool ) error {
928
897
ac .mu .Lock ()
929
- if ac .state == Shutdown {
898
+ if ac .state == connectivity . Shutdown {
930
899
ac .mu .Unlock ()
931
900
return errConnClosing
932
901
}
@@ -936,7 +905,7 @@ func (ac *addrConn) resetTransport(drain bool) error {
936
905
ac .down = nil
937
906
}
938
907
oldState := ac .state
939
- ac .state = Connecting
908
+ ac .state = connectivity . Connecting
940
909
ac .csEvltr .recordTransition (oldState , ac .state )
941
910
t := ac .transport
942
911
ac .transport = nil
@@ -949,7 +918,7 @@ func (ac *addrConn) resetTransport(drain bool) error {
949
918
ac .cc .mu .RUnlock ()
950
919
for retries := 0 ; ; retries ++ {
951
920
ac .mu .Lock ()
952
- if ac .state == Shutdown {
921
+ if ac .state == connectivity . Shutdown {
953
922
// ac.tearDown(...) has been invoked.
954
923
ac .mu .Unlock ()
955
924
return errConnClosing
@@ -977,14 +946,14 @@ func (ac *addrConn) resetTransport(drain bool) error {
977
946
}
978
947
grpclog .Warningf ("grpc: addrConn.resetTransport failed to create client transport: %v; Reconnecting to %v" , err , ac .addr )
979
948
ac .mu .Lock ()
980
- if ac .state == Shutdown {
949
+ if ac .state == connectivity . Shutdown {
981
950
// ac.tearDown(...) has been invoked.
982
951
ac .mu .Unlock ()
983
952
return errConnClosing
984
953
}
985
954
ac .errorf ("transient failure: %v" , err )
986
955
oldState = ac .state
987
- ac .state = TransientFailure
956
+ ac .state = connectivity . TransientFailure
988
957
ac .csEvltr .recordTransition (oldState , ac .state )
989
958
if ac .ready != nil {
990
959
close (ac .ready )
@@ -1003,14 +972,14 @@ func (ac *addrConn) resetTransport(drain bool) error {
1003
972
}
1004
973
ac .mu .Lock ()
1005
974
ac .printf ("ready" )
1006
- if ac .state == Shutdown {
975
+ if ac .state == connectivity . Shutdown {
1007
976
// ac.tearDown(...) has been invoked.
1008
977
ac .mu .Unlock ()
1009
978
newTransport .Close ()
1010
979
return errConnClosing
1011
980
}
1012
981
oldState = ac .state
1013
- ac .state = Ready
982
+ ac .state = connectivity . Ready
1014
983
ac .csEvltr .recordTransition (oldState , ac .state )
1015
984
ac .transport = newTransport
1016
985
if ac .ready != nil {
@@ -1081,13 +1050,13 @@ func (ac *addrConn) transportMonitor() {
1081
1050
default :
1082
1051
}
1083
1052
ac .mu .Lock ()
1084
- if ac .state == Shutdown {
1053
+ if ac .state == connectivity . Shutdown {
1085
1054
// ac has been shutdown.
1086
1055
ac .mu .Unlock ()
1087
1056
return
1088
1057
}
1089
1058
oldState := ac .state
1090
- ac .state = TransientFailure
1059
+ ac .state = connectivity . TransientFailure
1091
1060
ac .csEvltr .recordTransition (oldState , ac .state )
1092
1061
ac .mu .Unlock ()
1093
1062
if err := ac .resetTransport (false ); err != nil {
@@ -1107,12 +1076,12 @@ func (ac *addrConn) transportMonitor() {
1107
1076
}
1108
1077
1109
1078
// wait blocks until i) the new transport is up or ii) ctx is done or iii) ac is closed or
1110
- // iv) transport is in TransientFailure and there is a balancer/failfast is true.
1079
+ // iv) transport is in connectivity. TransientFailure and there is a balancer/failfast is true.
1111
1080
func (ac * addrConn ) wait (ctx context.Context , hasBalancer , failfast bool ) (transport.ClientTransport , error ) {
1112
1081
for {
1113
1082
ac .mu .Lock ()
1114
1083
switch {
1115
- case ac .state == Shutdown :
1084
+ case ac .state == connectivity . Shutdown :
1116
1085
if failfast || ! hasBalancer {
1117
1086
// RPC is failfast or balancer is nil. This RPC should fail with ac.tearDownErr.
1118
1087
err := ac .tearDownErr
@@ -1121,11 +1090,11 @@ func (ac *addrConn) wait(ctx context.Context, hasBalancer, failfast bool) (trans
1121
1090
}
1122
1091
ac .mu .Unlock ()
1123
1092
return nil , errConnClosing
1124
- case ac .state == Ready :
1093
+ case ac .state == connectivity . Ready :
1125
1094
ct := ac .transport
1126
1095
ac .mu .Unlock ()
1127
1096
return ct , nil
1128
- case ac .state == TransientFailure :
1097
+ case ac .state == connectivity . TransientFailure :
1129
1098
if failfast || hasBalancer {
1130
1099
ac .mu .Unlock ()
1131
1100
return nil , errConnUnavailable
@@ -1167,11 +1136,11 @@ func (ac *addrConn) tearDown(err error) {
1167
1136
// address removal and GoAway.
1168
1137
ac .transport .GracefulClose ()
1169
1138
}
1170
- if ac .state == Shutdown {
1139
+ if ac .state == connectivity . Shutdown {
1171
1140
return
1172
1141
}
1173
1142
oldState := ac .state
1174
- ac .state = Shutdown
1143
+ ac .state = connectivity . Shutdown
1175
1144
ac .tearDownErr = err
1176
1145
ac .csEvltr .recordTransition (oldState , ac .state )
1177
1146
if ac .events != nil {
0 commit comments