@@ -130,14 +130,7 @@ final class HTTPConnectionPool {
130130 }
131131
132132 private let stateLock = Lock ( )
133- private var _state : StateMachine {
134- didSet {
135- self . logger. trace ( " Connection Pool State changed " , metadata: [
136- " key " : " \( self . key) " ,
137- " state " : " \( self . _state) " ,
138- ] )
139- }
140- }
133+ private var _state : StateMachine
141134
142135 private static let fallbackConnectTimeout : TimeAmount = . seconds( 30 )
143136
@@ -175,6 +168,8 @@ final class HTTPConnectionPool {
175168 self . clientConfiguration = clientConfiguration
176169 self . key = key
177170 self . delegate = delegate
171+ var logger = logger
172+ logger [ metadataKey: " ahc-pool-key " ] = " \( key) "
178173 self . logger = logger
179174
180175 self . idleConnectionTimeout = clientConfiguration. connectionPool. idleTimeout
@@ -222,7 +217,11 @@ final class HTTPConnectionPool {
222217 self . cancelIdleTimerForConnection ( connectionID)
223218
224219 case . closeConnection( let connection, isShutdown: let isShutdown) :
225- // we are not interested in the close future...
220+ self . logger. trace ( " close connection " , metadata: [
221+ " ahc-connection-id " : " \( connection. id) " ,
222+ ] )
223+
224+ // we are not interested in the close promise...
226225 connection. close ( promise: nil )
227226
228227 if case . yes( let unclean) = isShutdown {
@@ -289,6 +288,9 @@ final class HTTPConnectionPool {
289288 }
290289
291290 private func createConnection( _ connectionID: Connection . ID , on eventLoop: EventLoop ) {
291+ self . logger. trace ( " Opening fresh connection " , metadata: [
292+ " ahc-connection-id " : " \( connectionID) " ,
293+ ] )
292294 // Even though this function is called make it actually creates/establishes a connection.
293295 // TBD: Should we rename it? To what?
294296 self . connectionFactory. makeConnection (
@@ -353,6 +355,9 @@ final class HTTPConnectionPool {
353355 }
354356
355357 private func scheduleIdleTimerForConnection( _ connectionID: Connection . ID , on eventLoop: EventLoop ) {
358+ self . logger. trace ( " Schedule idle connection timeout timer " , metadata: [
359+ " ahc-connection-id " : " \( connectionID) " ,
360+ ] )
356361 let scheduled = eventLoop. scheduleTask ( in: self . idleConnectionTimeout) {
357362 // there might be a race between a cancelTimer call and the triggering
358363 // of this scheduled task. both want to acquire the lock
@@ -375,6 +380,10 @@ final class HTTPConnectionPool {
375380 }
376381
377382 private func cancelIdleTimerForConnection( _ connectionID: Connection . ID ) {
383+ self . logger. trace ( " Cancel idle connection timeout timer " , metadata: [
384+ " ahc-connection-id " : " \( connectionID) " ,
385+ ] )
386+
378387 let cancelTimer = self . timerLock. withLock {
379388 self . _idleTimer. removeValue ( forKey: connectionID)
380389 }
@@ -387,6 +396,10 @@ final class HTTPConnectionPool {
387396 _ timeAmount: TimeAmount ,
388397 on eventLoop: EventLoop
389398 ) {
399+ self . logger. trace ( " Schedule connection creation backoff timer " , metadata: [
400+ " ahc-connection-id " : " \( connectionID) " ,
401+ ] )
402+
390403 let scheduled = eventLoop. scheduleTask ( in: timeAmount) {
391404 // there might be a race between a backoffTimer and the pool shutting down.
392405 let timerExisted = self . timerLock. withLock {
@@ -420,6 +433,10 @@ final class HTTPConnectionPool {
420433
421434extension HTTPConnectionPool : HTTPConnectionRequester {
422435 func http1ConnectionCreated( _ connection: HTTP1Connection ) {
436+ self . logger. trace ( " successfully created connection " , metadata: [
437+ " ahc-connection-id " : " \( connection. id) " ,
438+ " ahc-http-version " : " http/1.1 " ,
439+ ] )
423440 let action = self . stateLock. withLock {
424441 self . _state. newHTTP1ConnectionCreated ( . http1_1( connection) )
425442 }
@@ -442,6 +459,10 @@ extension HTTPConnectionPool: HTTPConnectionRequester {
442459 }
443460
444461 func failedToCreateHTTPConnection( _ connectionID: HTTPConnectionPool . Connection . ID , error: Error ) {
462+ self . logger. debug ( " connection attempt failed " , metadata: [
463+ " ahc-error " : " \( error) " ,
464+ " ahc-connection-id " : " \( connectionID) " ,
465+ ] )
445466 let action = self . stateLock. withLock {
446467 self . _state. failedToCreateNewConnection ( error, connectionID: connectionID)
447468 }
@@ -451,13 +472,21 @@ extension HTTPConnectionPool: HTTPConnectionRequester {
451472
452473extension HTTPConnectionPool : HTTP1ConnectionDelegate {
453474 func http1ConnectionClosed( _ connection: HTTP1Connection ) {
475+ self . logger. debug ( " connection closed " , metadata: [
476+ " ahc-connection-id " : " \( connection. id) " ,
477+ " ahc-http-version " : " http/1.1 " ,
478+ ] )
454479 let action = self . stateLock. withLock {
455480 self . _state. connectionClosed ( connection. id)
456481 }
457482 self . run ( action: action)
458483 }
459484
460485 func http1ConnectionReleased( _ connection: HTTP1Connection ) {
486+ self . logger. trace ( " releasing connection " , metadata: [
487+ " ahc-connection-id " : " \( connection. id) " ,
488+ " ahc-http-version " : " http/1.1 " ,
489+ ] )
461490 let action = self . stateLock. withLock {
462491 self . _state. http1ConnectionReleased ( connection. id)
463492 }
0 commit comments