2
0

ConnectionPoolDelegates.swift 5.5 KB

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