conn_state_evaluator.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. *
  3. * Copyright 2022 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package balancer
  19. import "google.golang.org/grpc/connectivity"
  20. // ConnectivityStateEvaluator takes the connectivity states of multiple SubConns
  21. // and returns one aggregated connectivity state.
  22. //
  23. // It's not thread safe.
  24. type ConnectivityStateEvaluator struct {
  25. numReady uint64 // Number of addrConns in ready state.
  26. numConnecting uint64 // Number of addrConns in connecting state.
  27. numTransientFailure uint64 // Number of addrConns in transient failure state.
  28. numIdle uint64 // Number of addrConns in idle state.
  29. }
  30. // RecordTransition records state change happening in subConn and based on that
  31. // it evaluates what aggregated state should be.
  32. //
  33. // - If at least one SubConn in Ready, the aggregated state is Ready;
  34. // - Else if at least one SubConn in Connecting, the aggregated state is Connecting;
  35. // - Else if at least one SubConn is Idle, the aggregated state is Idle;
  36. // - Else if at least one SubConn is TransientFailure (or there are no SubConns), the aggregated state is Transient Failure.
  37. //
  38. // Shutdown is not considered.
  39. func (cse *ConnectivityStateEvaluator) RecordTransition(oldState, newState connectivity.State) connectivity.State {
  40. // Update counters.
  41. for idx, state := range []connectivity.State{oldState, newState} {
  42. updateVal := 2*uint64(idx) - 1 // -1 for oldState and +1 for new.
  43. switch state {
  44. case connectivity.Ready:
  45. cse.numReady += updateVal
  46. case connectivity.Connecting:
  47. cse.numConnecting += updateVal
  48. case connectivity.TransientFailure:
  49. cse.numTransientFailure += updateVal
  50. case connectivity.Idle:
  51. cse.numIdle += updateVal
  52. }
  53. }
  54. return cse.CurrentState()
  55. }
  56. // CurrentState returns the current aggregate conn state by evaluating the counters
  57. func (cse *ConnectivityStateEvaluator) CurrentState() connectivity.State {
  58. // Evaluate.
  59. if cse.numReady > 0 {
  60. return connectivity.Ready
  61. }
  62. if cse.numConnecting > 0 {
  63. return connectivity.Connecting
  64. }
  65. if cse.numIdle > 0 {
  66. return connectivity.Idle
  67. }
  68. return connectivity.TransientFailure
  69. }