connectivity.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. *
  3. * Copyright 2017 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 connectivity defines connectivity semantics.
  19. // For details, see https://github.com/grpc/grpc/blob/master/doc/connectivity-semantics-and-api.md.
  20. // All APIs in this package are experimental.
  21. package connectivity
  22. import (
  23. "golang.org/x/net/context"
  24. "google.golang.org/grpc/grpclog"
  25. )
  26. // State indicates the state of connectivity.
  27. // It can be the state of a ClientConn or SubConn.
  28. type State int
  29. func (s State) String() string {
  30. switch s {
  31. case Idle:
  32. return "IDLE"
  33. case Connecting:
  34. return "CONNECTING"
  35. case Ready:
  36. return "READY"
  37. case TransientFailure:
  38. return "TRANSIENT_FAILURE"
  39. case Shutdown:
  40. return "SHUTDOWN"
  41. default:
  42. grpclog.Errorf("unknown connectivity state: %d", s)
  43. return "Invalid-State"
  44. }
  45. }
  46. const (
  47. // Idle indicates the ClientConn is idle.
  48. Idle State = iota
  49. // Connecting indicates the ClienConn is connecting.
  50. Connecting
  51. // Ready indicates the ClientConn is ready for work.
  52. Ready
  53. // TransientFailure indicates the ClientConn has seen a failure but expects to recover.
  54. TransientFailure
  55. // Shutdown indicates the ClientConn has started shutting down.
  56. Shutdown
  57. )
  58. // Reporter reports the connectivity states.
  59. type Reporter interface {
  60. // CurrentState returns the current state of the reporter.
  61. CurrentState() State
  62. // WaitForStateChange blocks until the reporter's state is different from the given state,
  63. // and returns true.
  64. // It returns false if <-ctx.Done() can proceed (ctx got timeout or got canceled).
  65. WaitForStateChange(context.Context, State) bool
  66. }