ChannelConnectivityState.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 2016, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #if SWIFT_PACKAGE
  17. import CgRPC
  18. #endif
  19. extension Channel {
  20. /// The connectivity state of a given gRPC channel.
  21. public enum ConnectivityState {
  22. /// Channel has just been initialized
  23. case initialized
  24. /// Channel is idle
  25. case idle
  26. /// Channel is connecting
  27. case connecting
  28. /// Channel is ready for work
  29. case ready
  30. /// Channel has seen a failure but expects to recover
  31. case transientFailure
  32. /// Channel has seen a failure that it cannot recover from
  33. case shutdown
  34. /// Channel connectivity state is unknown
  35. case unknown
  36. init(_ underlyingState: grpc_connectivity_state) {
  37. switch underlyingState {
  38. case GRPC_CHANNEL_INIT:
  39. self = .initialized
  40. case GRPC_CHANNEL_IDLE:
  41. self = .idle
  42. case GRPC_CHANNEL_CONNECTING:
  43. self = .connecting
  44. case GRPC_CHANNEL_READY:
  45. self = .ready
  46. case GRPC_CHANNEL_TRANSIENT_FAILURE:
  47. self = .transientFailure
  48. case GRPC_CHANNEL_SHUTDOWN:
  49. self = .shutdown
  50. default:
  51. self = .unknown
  52. }
  53. }
  54. var underlyingState: grpc_connectivity_state? {
  55. switch self {
  56. case .initialized:
  57. return GRPC_CHANNEL_INIT
  58. case .idle:
  59. return GRPC_CHANNEL_IDLE
  60. case .connecting:
  61. return GRPC_CHANNEL_CONNECTING
  62. case .ready:
  63. return GRPC_CHANNEL_READY
  64. case .transientFailure:
  65. return GRPC_CHANNEL_TRANSIENT_FAILURE
  66. case .shutdown:
  67. return GRPC_CHANNEL_SHUTDOWN
  68. default:
  69. return nil
  70. }
  71. }
  72. }
  73. }