ConnectionPoolDelegates.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. case stats([GRPCSubPoolStats], GRPCConnectionPoolID)
  90. var id: GRPCConnectionID? {
  91. switch self {
  92. case let .connectionAdded(id),
  93. let .startedConnecting(id),
  94. let .connectFailed(id),
  95. let .connectSucceeded(id, _),
  96. let .connectionClosed(id),
  97. let .connectionUtilizationChanged(id, _, _),
  98. let .connectionQuiescing(id),
  99. let .connectionRemoved(id):
  100. return id
  101. case .stats:
  102. return nil
  103. }
  104. }
  105. }
  106. private var events: CircularBuffer<Event> = []
  107. private let lock = NIOLock()
  108. var first: Event? {
  109. return self.lock.withLock {
  110. self.events.first
  111. }
  112. }
  113. var isEmpty: Bool {
  114. return self.lock.withLock { self.events.isEmpty }
  115. }
  116. func popFirst() -> Event? {
  117. return self.lock.withLock {
  118. self.events.popFirst()
  119. }
  120. }
  121. func removeAll() -> CircularBuffer<Event> {
  122. return self.lock.withLock {
  123. defer { self.events.removeAll() }
  124. return self.events
  125. }
  126. }
  127. func connectionAdded(id: GRPCConnectionID) {
  128. self.lock.withLock {
  129. self.events.append(.connectionAdded(id))
  130. }
  131. }
  132. func startedConnecting(id: GRPCConnectionID) {
  133. self.lock.withLock {
  134. self.events.append(.startedConnecting(id))
  135. }
  136. }
  137. func connectFailed(id: GRPCConnectionID, error: Error) {
  138. self.lock.withLock {
  139. self.events.append(.connectFailed(id))
  140. }
  141. }
  142. func connectSucceeded(id: GRPCConnectionID, streamCapacity: Int) {
  143. self.lock.withLock {
  144. self.events.append(.connectSucceeded(id, streamCapacity))
  145. }
  146. }
  147. func connectionClosed(id: GRPCConnectionID, error: Error?) {
  148. self.lock.withLock {
  149. self.events.append(.connectionClosed(id))
  150. }
  151. }
  152. func connectionUtilizationChanged(id: GRPCConnectionID, streamsUsed: Int, streamCapacity: Int) {
  153. self.lock.withLock {
  154. self.events.append(.connectionUtilizationChanged(id, streamsUsed, streamCapacity))
  155. }
  156. }
  157. func connectionQuiescing(id: GRPCConnectionID) {
  158. self.lock.withLock {
  159. self.events.append(.connectionQuiescing(id))
  160. }
  161. }
  162. func connectionRemoved(id: GRPCConnectionID) {
  163. self.lock.withLock {
  164. self.events.append(.connectionRemoved(id))
  165. }
  166. }
  167. func connectionPoolStats(_ stats: [GRPCSubPoolStats], id: GRPCConnectionPoolID) {
  168. self.lock.withLock {
  169. self.events.append(.stats(stats, id))
  170. }
  171. }
  172. }
  173. extension EventRecordingConnectionPoolDelegate: @unchecked Sendable {}