ConnectionPoolDelegates.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright 2022, 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 GRPC
  17. import NIOConcurrencyHelpers
  18. import NIOCore
  19. final class IsConnectingDelegate: GRPCConnectionPoolDelegate {
  20. private let lock = NIOLock()
  21. private var connecting = Set<GRPCConnectionID>()
  22. private var active = Set<GRPCConnectionID>()
  23. enum StateNotifacation: Hashable, Sendable {
  24. case connecting
  25. case connected
  26. }
  27. private let onStateChange: @Sendable (StateNotifacation) -> Void
  28. init(onStateChange: @escaping @Sendable (StateNotifacation) -> Void) {
  29. self.onStateChange = onStateChange
  30. }
  31. func startedConnecting(id: GRPCConnectionID) {
  32. let didStartConnecting: Bool = self.lock.withLock {
  33. let (inserted, _) = self.connecting.insert(id)
  34. // Only intereseted new connection attempts when there are no active connections.
  35. return inserted && self.connecting.count == 1 && self.active.isEmpty
  36. }
  37. if didStartConnecting {
  38. self.onStateChange(.connecting)
  39. }
  40. }
  41. func connectSucceeded(id: GRPCConnectionID, streamCapacity: Int) {
  42. let didStopConnecting: Bool = self.lock.withLock {
  43. let removed = self.connecting.remove(id) != nil
  44. let (inserted, _) = self.active.insert(id)
  45. return removed && inserted && self.active.count == 1
  46. }
  47. if didStopConnecting {
  48. self.onStateChange(.connected)
  49. }
  50. }
  51. func connectionClosed(id: GRPCConnectionID, error: Error?) {
  52. self.lock.withLock {
  53. self.active.remove(id)
  54. self.connecting.remove(id)
  55. }
  56. }
  57. func connectionQuiescing(id: GRPCConnectionID) {
  58. self.lock.withLock {
  59. _ = self.active.remove(id)
  60. }
  61. }
  62. // No-op.
  63. func connectionAdded(id: GRPCConnectionID) {}
  64. // No-op.
  65. func connectionRemoved(id: GRPCConnectionID) {}
  66. // Conection failures put the connection into a backing off state, we consider that to still
  67. // be 'connecting' at this point.
  68. func connectFailed(id: GRPCConnectionID, error: Error) {}
  69. // No-op.
  70. func connectionUtilizationChanged(id: GRPCConnectionID, streamsUsed: Int, streamCapacity: Int) {}
  71. }
  72. extension IsConnectingDelegate: @unchecked Sendable {}
  73. final class EventRecordingConnectionPoolDelegate: GRPCConnectionPoolDelegate {
  74. struct UnexpectedEvent: Error {
  75. var event: Event
  76. init(_ event: Event) {
  77. self.event = event
  78. }
  79. }
  80. enum Event: Equatable {
  81. case connectionAdded(GRPCConnectionID)
  82. case startedConnecting(GRPCConnectionID)
  83. case connectFailed(GRPCConnectionID)
  84. case connectSucceeded(GRPCConnectionID, Int)
  85. case connectionClosed(GRPCConnectionID)
  86. case connectionUtilizationChanged(GRPCConnectionID, Int, Int)
  87. case connectionQuiescing(GRPCConnectionID)
  88. case connectionRemoved(GRPCConnectionID)
  89. var id: GRPCConnectionID {
  90. switch self {
  91. case let .connectionAdded(id),
  92. let .startedConnecting(id),
  93. let .connectFailed(id),
  94. let .connectSucceeded(id, _),
  95. let .connectionClosed(id),
  96. let .connectionUtilizationChanged(id, _, _),
  97. let .connectionQuiescing(id),
  98. let .connectionRemoved(id):
  99. return id
  100. }
  101. }
  102. }
  103. private var events: CircularBuffer<Event> = []
  104. private let lock = NIOLock()
  105. var first: Event? {
  106. return self.lock.withLock {
  107. self.events.first
  108. }
  109. }
  110. var isEmpty: Bool {
  111. return self.lock.withLock { self.events.isEmpty }
  112. }
  113. func popFirst() -> Event? {
  114. return self.lock.withLock {
  115. self.events.popFirst()
  116. }
  117. }
  118. func connectionAdded(id: GRPCConnectionID) {
  119. self.lock.withLock {
  120. self.events.append(.connectionAdded(id))
  121. }
  122. }
  123. func startedConnecting(id: GRPCConnectionID) {
  124. self.lock.withLock {
  125. self.events.append(.startedConnecting(id))
  126. }
  127. }
  128. func connectFailed(id: GRPCConnectionID, error: Error) {
  129. self.lock.withLock {
  130. self.events.append(.connectFailed(id))
  131. }
  132. }
  133. func connectSucceeded(id: GRPCConnectionID, streamCapacity: Int) {
  134. self.lock.withLock {
  135. self.events.append(.connectSucceeded(id, streamCapacity))
  136. }
  137. }
  138. func connectionClosed(id: GRPCConnectionID, error: Error?) {
  139. self.lock.withLock {
  140. self.events.append(.connectionClosed(id))
  141. }
  142. }
  143. func connectionUtilizationChanged(id: GRPCConnectionID, streamsUsed: Int, streamCapacity: Int) {
  144. self.lock.withLock {
  145. self.events.append(.connectionUtilizationChanged(id, streamsUsed, streamCapacity))
  146. }
  147. }
  148. func connectionQuiescing(id: GRPCConnectionID) {
  149. self.lock.withLock {
  150. self.events.append(.connectionQuiescing(id))
  151. }
  152. }
  153. func connectionRemoved(id: GRPCConnectionID) {
  154. self.lock.withLock {
  155. self.events.append(.connectionRemoved(id))
  156. }
  157. }
  158. }
  159. extension EventRecordingConnectionPoolDelegate: @unchecked Sendable {}
  160. final class AsyncEventStreamConnectionPoolDelegate: GRPCConnectionPoolDelegate {
  161. private let continuation: AsyncStream<EventRecordingConnectionPoolDelegate.Event>.Continuation
  162. static func makeDelegateAndAsyncStream()
  163. -> (
  164. AsyncEventStreamConnectionPoolDelegate,
  165. AsyncStream<EventRecordingConnectionPoolDelegate.Event>
  166. ) {
  167. var continuation: AsyncStream<EventRecordingConnectionPoolDelegate.Event>.Continuation!
  168. let asyncStream = AsyncStream(EventRecordingConnectionPoolDelegate.Event.self) {
  169. continuation = $0
  170. }
  171. return (Self(continuation: continuation), asyncStream)
  172. }
  173. init(continuation: AsyncStream<EventRecordingConnectionPoolDelegate.Event>.Continuation) {
  174. self.continuation = continuation
  175. }
  176. func connectionAdded(id: GRPCConnectionID) {
  177. self.continuation.yield(.connectionAdded(id))
  178. }
  179. func startedConnecting(id: GRPCConnectionID) {
  180. self.continuation.yield(.startedConnecting(id))
  181. }
  182. func connectFailed(id: GRPCConnectionID, error: Error) {
  183. self.continuation.yield(.connectFailed(id))
  184. }
  185. func connectSucceeded(id: GRPCConnectionID, streamCapacity: Int) {
  186. self.continuation.yield(.connectSucceeded(id, streamCapacity))
  187. }
  188. func connectionClosed(id: GRPCConnectionID, error: Error?) {
  189. self.continuation.yield(.connectionClosed(id))
  190. }
  191. func connectionUtilizationChanged(id: GRPCConnectionID, streamsUsed: Int, streamCapacity: Int) {
  192. self.continuation.yield(.connectionUtilizationChanged(id, streamsUsed, streamCapacity))
  193. }
  194. func connectionQuiescing(id: GRPCConnectionID) {
  195. self.continuation.yield(.connectionQuiescing(id))
  196. }
  197. func connectionRemoved(id: GRPCConnectionID) {
  198. self.continuation.yield(.connectionRemoved(id))
  199. }
  200. }