@@ -130,14 +130,7 @@ final class HTTPConnectionPool {
130
130
}
131
131
132
132
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
141
134
142
135
private static let fallbackConnectTimeout : TimeAmount = . seconds( 30 )
143
136
@@ -175,6 +168,8 @@ final class HTTPConnectionPool {
175
168
self . clientConfiguration = clientConfiguration
176
169
self . key = key
177
170
self . delegate = delegate
171
+ var logger = logger
172
+ logger [ metadataKey: " ahc-pool-key " ] = " \( key) "
178
173
self . logger = logger
179
174
180
175
self . idleConnectionTimeout = clientConfiguration. connectionPool. idleTimeout
@@ -222,7 +217,11 @@ final class HTTPConnectionPool {
222
217
self . cancelIdleTimerForConnection ( connectionID)
223
218
224
219
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...
226
225
connection. close ( promise: nil )
227
226
228
227
if case . yes( let unclean) = isShutdown {
@@ -289,6 +288,9 @@ final class HTTPConnectionPool {
289
288
}
290
289
291
290
private func createConnection( _ connectionID: Connection . ID , on eventLoop: EventLoop ) {
291
+ self . logger. trace ( " Opening fresh connection " , metadata: [
292
+ " ahc-connection-id " : " \( connectionID) " ,
293
+ ] )
292
294
// Even though this function is called make it actually creates/establishes a connection.
293
295
// TBD: Should we rename it? To what?
294
296
self . connectionFactory. makeConnection (
@@ -353,6 +355,9 @@ final class HTTPConnectionPool {
353
355
}
354
356
355
357
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
+ ] )
356
361
let scheduled = eventLoop. scheduleTask ( in: self . idleConnectionTimeout) {
357
362
// there might be a race between a cancelTimer call and the triggering
358
363
// of this scheduled task. both want to acquire the lock
@@ -375,6 +380,10 @@ final class HTTPConnectionPool {
375
380
}
376
381
377
382
private func cancelIdleTimerForConnection( _ connectionID: Connection . ID ) {
383
+ self . logger. trace ( " Cancel idle connection timeout timer " , metadata: [
384
+ " ahc-connection-id " : " \( connectionID) " ,
385
+ ] )
386
+
378
387
let cancelTimer = self . timerLock. withLock {
379
388
self . _idleTimer. removeValue ( forKey: connectionID)
380
389
}
@@ -387,6 +396,10 @@ final class HTTPConnectionPool {
387
396
_ timeAmount: TimeAmount ,
388
397
on eventLoop: EventLoop
389
398
) {
399
+ self . logger. trace ( " Schedule connection creation backoff timer " , metadata: [
400
+ " ahc-connection-id " : " \( connectionID) " ,
401
+ ] )
402
+
390
403
let scheduled = eventLoop. scheduleTask ( in: timeAmount) {
391
404
// there might be a race between a backoffTimer and the pool shutting down.
392
405
let timerExisted = self . timerLock. withLock {
@@ -420,6 +433,10 @@ final class HTTPConnectionPool {
420
433
421
434
extension HTTPConnectionPool : HTTPConnectionRequester {
422
435
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
+ ] )
423
440
let action = self . stateLock. withLock {
424
441
self . _state. newHTTP1ConnectionCreated ( . http1_1( connection) )
425
442
}
@@ -442,6 +459,10 @@ extension HTTPConnectionPool: HTTPConnectionRequester {
442
459
}
443
460
444
461
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
+ ] )
445
466
let action = self . stateLock. withLock {
446
467
self . _state. failedToCreateNewConnection ( error, connectionID: connectionID)
447
468
}
@@ -451,13 +472,21 @@ extension HTTPConnectionPool: HTTPConnectionRequester {
451
472
452
473
extension HTTPConnectionPool : HTTP1ConnectionDelegate {
453
474
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
+ ] )
454
479
let action = self . stateLock. withLock {
455
480
self . _state. connectionClosed ( connection. id)
456
481
}
457
482
self . run ( action: action)
458
483
}
459
484
460
485
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
+ ] )
461
490
let action = self . stateLock. withLock {
462
491
self . _state. http1ConnectionReleased ( connection. id)
463
492
}
0 commit comments