-
Notifications
You must be signed in to change notification settings - Fork 130
Add logging to the new HTTPConnectionPool #428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ final class HTTP1ClientChannelHandler: ChannelDuplexHandler { | |
if let newRequest = self.request { | ||
var requestLogger = newRequest.logger | ||
requestLogger[metadataKey: "ahc-connection-id"] = "\(self.connection.id)" | ||
requestLogger[metadataKey: "ahc-el"] = "\(self.connection.channel.eventLoop)" | ||
self.logger = requestLogger | ||
|
||
if let idleReadTimeout = newRequest.idleReadTimeout { | ||
|
@@ -120,15 +121,15 @@ final class HTTP1ClientChannelHandler: ChannelDuplexHandler { | |
} | ||
|
||
func channelReadComplete(context: ChannelHandlerContext) { | ||
self.logger.trace("Read complete caught") | ||
self.logger.trace("Channel read complete caught") | ||
|
||
let action = self.state.channelReadComplete() | ||
self.run(action, context: context) | ||
} | ||
|
||
func errorCaught(context: ChannelHandlerContext, error: Error) { | ||
self.logger.trace("Error caught", metadata: [ | ||
"error": "\(error)", | ||
self.logger.trace("Channel error caught", metadata: [ | ||
"ahc-error": "\(error)", | ||
]) | ||
|
||
let action = self.state.errorHappened(error) | ||
|
@@ -142,12 +143,7 @@ final class HTTP1ClientChannelHandler: ChannelDuplexHandler { | |
let req = self.unwrapOutboundIn(data) | ||
self.request = req | ||
|
||
self.logger.trace("New request to execute") | ||
|
||
if let idleReadTimeout = self.request?.idleReadTimeout { | ||
self.idleReadTimeoutStateMachine = .init(timeAmount: idleReadTimeout) | ||
} | ||
Comment on lines
-147
to
-149
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be removed, since it is already included in the request setter ( |
||
|
||
self.logger.debug("Request was scheduled on connection") | ||
req.willExecuteRequest(self) | ||
|
||
let action = self.state.runNewRequest(head: req.requestHead, metadata: req.requestFramingMetadata) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,14 +130,7 @@ final class HTTPConnectionPool { | |
} | ||
|
||
private let stateLock = Lock() | ||
private var _state: StateMachine { | ||
didSet { | ||
self.logger.trace("Connection Pool State changed", metadata: [ | ||
"key": "\(self.key)", | ||
"state": "\(self._state)", | ||
]) | ||
} | ||
Comment on lines
-134
to
-139
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed, since it produces way too many logs. We should solely focus on recording the events so that we can replay the finite state machine if needed. |
||
} | ||
private var _state: StateMachine | ||
|
||
private static let fallbackConnectTimeout: TimeAmount = .seconds(30) | ||
|
||
|
@@ -175,6 +168,8 @@ final class HTTPConnectionPool { | |
self.clientConfiguration = clientConfiguration | ||
self.key = key | ||
self.delegate = delegate | ||
var logger = logger | ||
logger[metadataKey: "ahc-pool-key"] = "\(key)" | ||
self.logger = logger | ||
|
||
self.idleConnectionTimeout = clientConfiguration.connectionPool.idleTimeout | ||
|
@@ -222,7 +217,11 @@ final class HTTPConnectionPool { | |
self.cancelIdleTimerForConnection(connectionID) | ||
|
||
case .closeConnection(let connection, isShutdown: let isShutdown): | ||
// we are not interested in the close future... | ||
self.logger.trace("close connection", metadata: [ | ||
"ahc-connection-id": "\(connection.id)", | ||
]) | ||
|
||
// we are not interested in the close promise... | ||
connection.close(promise: nil) | ||
|
||
if case .yes(let unclean) = isShutdown { | ||
|
@@ -289,6 +288,9 @@ final class HTTPConnectionPool { | |
} | ||
|
||
private func createConnection(_ connectionID: Connection.ID, on eventLoop: EventLoop) { | ||
self.logger.trace("Opening fresh connection", metadata: [ | ||
"ahc-connection-id": "\(connectionID)", | ||
]) | ||
// Even though this function is called make it actually creates/establishes a connection. | ||
// TBD: Should we rename it? To what? | ||
self.connectionFactory.makeConnection( | ||
|
@@ -353,6 +355,9 @@ final class HTTPConnectionPool { | |
} | ||
|
||
private func scheduleIdleTimerForConnection(_ connectionID: Connection.ID, on eventLoop: EventLoop) { | ||
self.logger.trace("Schedule idle connection timeout timer", metadata: [ | ||
"ahc-connection-id": "\(connectionID)", | ||
]) | ||
let scheduled = eventLoop.scheduleTask(in: self.idleConnectionTimeout) { | ||
// there might be a race between a cancelTimer call and the triggering | ||
// of this scheduled task. both want to acquire the lock | ||
|
@@ -375,6 +380,10 @@ final class HTTPConnectionPool { | |
} | ||
|
||
private func cancelIdleTimerForConnection(_ connectionID: Connection.ID) { | ||
self.logger.trace("Cancel idle connection timeout timer", metadata: [ | ||
"ahc-connection-id": "\(connectionID)", | ||
]) | ||
|
||
let cancelTimer = self.timerLock.withLock { | ||
self._idleTimer.removeValue(forKey: connectionID) | ||
} | ||
|
@@ -387,6 +396,10 @@ final class HTTPConnectionPool { | |
_ timeAmount: TimeAmount, | ||
on eventLoop: EventLoop | ||
) { | ||
self.logger.trace("Schedule connection creation backoff timer", metadata: [ | ||
"ahc-connection-id": "\(connectionID)", | ||
]) | ||
|
||
let scheduled = eventLoop.scheduleTask(in: timeAmount) { | ||
// there might be a race between a backoffTimer and the pool shutting down. | ||
let timerExisted = self.timerLock.withLock { | ||
|
@@ -420,6 +433,10 @@ final class HTTPConnectionPool { | |
|
||
extension HTTPConnectionPool: HTTPConnectionRequester { | ||
func http1ConnectionCreated(_ connection: HTTP1Connection) { | ||
self.logger.trace("successfully created connection", metadata: [ | ||
"ahc-connection-id": "\(connection.id)", | ||
"ahc-http-version": "http/1.1", | ||
]) | ||
let action = self.stateLock.withLock { | ||
self._state.newHTTP1ConnectionCreated(.http1_1(connection)) | ||
} | ||
|
@@ -442,6 +459,10 @@ extension HTTPConnectionPool: HTTPConnectionRequester { | |
} | ||
|
||
func failedToCreateHTTPConnection(_ connectionID: HTTPConnectionPool.Connection.ID, error: Error) { | ||
self.logger.debug("connection attempt failed", metadata: [ | ||
"ahc-error": "\(error)", | ||
"ahc-connection-id": "\(connectionID)", | ||
]) | ||
let action = self.stateLock.withLock { | ||
self._state.failedToCreateNewConnection(error, connectionID: connectionID) | ||
} | ||
|
@@ -451,13 +472,21 @@ extension HTTPConnectionPool: HTTPConnectionRequester { | |
|
||
extension HTTPConnectionPool: HTTP1ConnectionDelegate { | ||
func http1ConnectionClosed(_ connection: HTTP1Connection) { | ||
self.logger.debug("connection closed", metadata: [ | ||
"ahc-connection-id": "\(connection.id)", | ||
"ahc-http-version": "http/1.1", | ||
]) | ||
let action = self.stateLock.withLock { | ||
self._state.connectionClosed(connection.id) | ||
} | ||
self.run(action: action) | ||
} | ||
|
||
func http1ConnectionReleased(_ connection: HTTP1Connection) { | ||
self.logger.trace("releasing connection", metadata: [ | ||
"ahc-connection-id": "\(connection.id)", | ||
"ahc-http-version": "http/1.1", | ||
]) | ||
let action = self.stateLock.withLock { | ||
self._state.http1ConnectionReleased(connection.id) | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.