@@ -103,7 +103,7 @@ internal final class ConnectionPool {
103
103
104
104
/// A logger which always sets "GRPC" as its source.
105
105
@usableFromInline
106
- internal let logger : GRPCLogger
106
+ private ( set ) var logger : GRPCLogger
107
107
108
108
/// Returns `NIODeadline` representing 'now'. This is useful for testing.
109
109
@usableFromInline
@@ -139,6 +139,18 @@ internal final class ConnectionPool {
139
139
/// The ID of waiter.
140
140
@usableFromInline
141
141
static let waiterID = " pool.waiter.id "
142
+ /// The maximum number of connections allowed in the pool.
143
+ @usableFromInline
144
+ static let connectionsMax = " pool.connections.max "
145
+ /// The number of connections in the ready state.
146
+ @usableFromInline
147
+ static let connectionsReady = " pool.connections.ready "
148
+ /// The number of connections in the connecting state.
149
+ @usableFromInline
150
+ static let connectionsConnecting = " pool.connections.connecting "
151
+ /// The number of connections in the transient failure state.
152
+ @usableFromInline
153
+ static let connectionsTransientFailure = " pool.connections.transientFailure "
142
154
}
143
155
144
156
@usableFromInline
@@ -158,6 +170,7 @@ internal final class ConnectionPool {
158
170
( 0.0 ... 1.0 ) . contains ( reservationLoadThreshold) ,
159
171
" reservationLoadThreshold must be within the range 0.0 ... 1.0 "
160
172
)
173
+
161
174
self . reservationLoadThreshold = reservationLoadThreshold
162
175
self . assumedMaxConcurrentStreams = assumedMaxConcurrentStreams
163
176
@@ -179,6 +192,14 @@ internal final class ConnectionPool {
179
192
/// - Parameter connections: The number of connections to add to the pool.
180
193
internal func initialize( connections: Int ) {
181
194
assert ( self . _connections. isEmpty)
195
+ self . logger. logger [ metadataKey: Metadata . id] = " \( ObjectIdentifier ( self ) ) "
196
+ self . logger. debug (
197
+ " initializing new sub-pool " ,
198
+ metadata: [
199
+ Metadata . waitersMax: . stringConvertible( self . maxWaiters) ,
200
+ Metadata . connectionsMax: . stringConvertible( connections) ,
201
+ ]
202
+ )
182
203
self . _connections. reserveCapacity ( connections)
183
204
while self . _connections. count < connections {
184
205
self . addConnectionToPool ( )
@@ -461,6 +482,20 @@ internal final class ConnectionPool {
461
482
internal func _startConnectingIdleConnection( ) {
462
483
if let index = self . _connections. values. firstIndex ( where: { $0. manager. sync. isIdle } ) {
463
484
self . _connections. values [ index] . manager. sync. startConnecting ( )
485
+ } else {
486
+ let connecting = self . _connections. values. count { $0. manager. sync. isConnecting }
487
+ let ready = self . _connections. values. count { $0. manager. sync. isReady }
488
+ let transientFailure = self . _connections. values. count { $0. manager. sync. isTransientFailure }
489
+
490
+ self . logger. debug (
491
+ " no idle connections in pool " ,
492
+ metadata: [
493
+ Metadata . connectionsConnecting: . stringConvertible( connecting) ,
494
+ Metadata . connectionsReady: . stringConvertible( ready) ,
495
+ Metadata . connectionsTransientFailure: . stringConvertible( transientFailure) ,
496
+ Metadata . waitersCount: . stringConvertible( self . waiters. count) ,
497
+ ]
498
+ )
464
499
}
465
500
}
466
501
@@ -965,3 +1000,11 @@ extension GRPCConnectionPoolError: GRPCStatusTransformable {
965
1000
}
966
1001
}
967
1002
}
1003
+
1004
+ extension Sequence {
1005
+ fileprivate func count( where predicate: ( Element ) -> Bool ) -> Int {
1006
+ return self . reduce ( 0 ) { count, element in
1007
+ predicate ( element) ? count + 1 : count
1008
+ }
1009
+ }
1010
+ }
0 commit comments