ConnectivityState.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright 2019, 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. import Foundation
  17. /// The connectivity state of a client connection. Note that this is heavily lifted from the gRPC
  18. /// documentation: https://github.com/grpc/grpc/blob/master/doc/connectivity-semantics-and-api.md.
  19. public enum ConnectivityState {
  20. /// This is the state where the channel has not yet been created.
  21. case idle
  22. /// The channel is trying to establish a connection and is waiting to make progress on one of the
  23. /// steps involved in name resolution, TCP connection establishment or TLS handshake.
  24. case connecting
  25. /// The channel has successfully established a connection all the way through TLS handshake (or
  26. /// equivalent) and protocol-level (HTTP/2, etc) handshaking.
  27. case ready
  28. /// There has been some transient failure (such as a TCP 3-way handshake timing out or a socket
  29. /// error). Channels in this state will eventually switch to the `.connecting` state and try to
  30. /// establish a connection again. Since retries are done with exponential backoff, channels that
  31. /// fail to connect will start out spending very little time in this state but as the attempts
  32. /// fail repeatedly, the channel will spend increasingly large amounts of time in this state.
  33. case transientFailure
  34. /// This channel has started shutting down. Any new RPCs should fail immediately. Pending RPCs
  35. /// may continue running till the application cancels them. Channels may enter this state either
  36. /// because the application explicitly requested a shutdown or if a non-recoverable error has
  37. /// happened during attempts to connect. Channels that have entered this state will never leave
  38. /// this state.
  39. case shutdown
  40. }
  41. public protocol ConnectivityStateDelegate: class {
  42. /// Called when a change in `ConnectivityState` has occurred.
  43. ///
  44. /// - Parameter oldState: The old connectivity state.
  45. /// - Parameter newState: The new connectivity state.
  46. func connectivityStateDidChange(from oldState: ConnectivityState, to newState: ConnectivityState)
  47. }
  48. public class ConnectivityStateMonitor {
  49. public typealias Callback = () -> Void
  50. private var idleCallback: Callback?
  51. private var connectingCallback: Callback?
  52. private var readyCallback: Callback?
  53. private var transientFailureCallback: Callback?
  54. private var shutdownCallback: Callback?
  55. /// A delegate to call when the connectivity state changes.
  56. public var delegate: ConnectivityStateDelegate?
  57. /// The current state of connectivity.
  58. public internal(set) var state: ConnectivityState {
  59. didSet {
  60. if oldValue != self.state {
  61. self.delegate?.connectivityStateDidChange(from: oldValue, to: self.state)
  62. self.triggerAndResetCallback()
  63. }
  64. }
  65. }
  66. /// Creates a new connectivity state monitor.
  67. ///
  68. /// - Parameter delegate: A delegate to call when the connectivity state changes.
  69. public init(delegate: ConnectivityStateDelegate?) {
  70. self.delegate = delegate
  71. self.state = .idle
  72. }
  73. /// Registers a callback on the given state and calls it the next time that state is observed.
  74. /// Subsequent transitions to that state will **not** trigger the callback.
  75. ///
  76. /// - Parameter state: The state on which to call the given callback.
  77. /// - Parameter callback: The closure to call once the given state has been transitioned to. The
  78. /// `callback` can be removed by passing in `nil`.
  79. public func onNext(state: ConnectivityState, callback: Callback?) {
  80. switch state {
  81. case .idle:
  82. self.idleCallback = callback
  83. case .connecting:
  84. self.connectingCallback = callback
  85. case .ready:
  86. self.readyCallback = callback
  87. case .transientFailure:
  88. self.transientFailureCallback = callback
  89. case .shutdown:
  90. self.shutdownCallback = callback
  91. }
  92. }
  93. private func triggerAndResetCallback() {
  94. switch self.state {
  95. case .idle:
  96. self.idleCallback?()
  97. self.idleCallback = nil
  98. case .connecting:
  99. self.connectingCallback?()
  100. self.connectingCallback = nil
  101. case .ready:
  102. self.readyCallback?()
  103. self.readyCallback = nil
  104. case .transientFailure:
  105. self.transientFailureCallback?()
  106. self.transientFailureCallback = nil
  107. case .shutdown:
  108. self.shutdownCallback?()
  109. self.shutdownCallback = nil
  110. }
  111. }
  112. }