connectivity.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2019 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package connectivity implements client connectivity operations.
  15. package connectivity
  16. import (
  17. "sync"
  18. "go.uber.org/zap"
  19. "google.golang.org/grpc/connectivity"
  20. )
  21. // Recorder records gRPC connectivity.
  22. type Recorder interface {
  23. GetCurrentState() connectivity.State
  24. RecordTransition(oldState, newState connectivity.State)
  25. }
  26. // New returns a new Recorder.
  27. func New(lg *zap.Logger) Recorder {
  28. return &recorder{lg: lg}
  29. }
  30. // recorder takes the connectivity states of multiple SubConns
  31. // and returns one aggregated connectivity state.
  32. // ref. https://github.com/grpc/grpc-go/blob/master/balancer/balancer.go
  33. type recorder struct {
  34. lg *zap.Logger
  35. mu sync.RWMutex
  36. cur connectivity.State
  37. numReady uint64 // Number of addrConns in ready state.
  38. numConnecting uint64 // Number of addrConns in connecting state.
  39. numTransientFailure uint64 // Number of addrConns in transientFailure.
  40. }
  41. func (rc *recorder) GetCurrentState() (state connectivity.State) {
  42. rc.mu.RLock()
  43. defer rc.mu.RUnlock()
  44. return rc.cur
  45. }
  46. // RecordTransition records state change happening in subConn and based on that
  47. // it evaluates what aggregated state should be.
  48. //
  49. // - If at least one SubConn in Ready, the aggregated state is Ready;
  50. // - Else if at least one SubConn in Connecting, the aggregated state is Connecting;
  51. // - Else the aggregated state is TransientFailure.
  52. //
  53. // Idle and Shutdown are not considered.
  54. //
  55. // ref. https://github.com/grpc/grpc-go/blob/master/balancer/balancer.go
  56. func (rc *recorder) RecordTransition(oldState, newState connectivity.State) {
  57. rc.mu.Lock()
  58. defer rc.mu.Unlock()
  59. for idx, state := range []connectivity.State{oldState, newState} {
  60. updateVal := 2*uint64(idx) - 1 // -1 for oldState and +1 for new.
  61. switch state {
  62. case connectivity.Ready:
  63. rc.numReady += updateVal
  64. case connectivity.Connecting:
  65. rc.numConnecting += updateVal
  66. case connectivity.TransientFailure:
  67. rc.numTransientFailure += updateVal
  68. default:
  69. rc.lg.Warn("connectivity recorder received unknown state", zap.String("connectivity-state", state.String()))
  70. }
  71. }
  72. switch { // must be exclusive, no overlap
  73. case rc.numReady > 0:
  74. rc.cur = connectivity.Ready
  75. case rc.numConnecting > 0:
  76. rc.cur = connectivity.Connecting
  77. default:
  78. rc.cur = connectivity.TransientFailure
  79. }
  80. }