-
Notifications
You must be signed in to change notification settings - Fork 133
Add "debug initializer" hook for channels #801
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 1 commit
9e26b5e
f51ae1c
a323796
0d22e92
4847b6e
d47e756
fec1b80
77ffedf
134aebf
003f9d8
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 |
---|---|---|
|
@@ -847,14 +847,32 @@ public class HTTPClient { | |
/// By default, don't use it | ||
public var enableMultipath: Bool | ||
|
||
/// A method with access to the HTTP/1 connection channel that is called when creating the connection. | ||
public var http1_1ConnectionDebugInitializer: | ||
(@Sendable (Channel) -> EventLoopFuture<Void>)? | ||
|
||
/// A method with access to the HTTP/2 connection channel that is called when creating the connection. | ||
public var http2ConnectionDebugInitializer: | ||
(@Sendable (Channel) -> EventLoopFuture<Void>)? | ||
|
||
/// A method with access to the HTTP/2 stream channel that is called when creating the stream. | ||
public var http2StreamChannelDebugInitializer: | ||
(@Sendable (Channel) -> EventLoopFuture<Void>)? | ||
|
||
public init( | ||
tlsConfiguration: TLSConfiguration? = nil, | ||
redirectConfiguration: RedirectConfiguration? = nil, | ||
timeout: Timeout = Timeout(), | ||
connectionPool: ConnectionPool = ConnectionPool(), | ||
proxy: Proxy? = nil, | ||
ignoreUncleanSSLShutdown: Bool = false, | ||
decompression: Decompression = .disabled | ||
decompression: Decompression = .disabled, | ||
http1_1ConnectionDebugInitializer: | ||
(@Sendable (Channel) -> EventLoopFuture<Void>)? = nil, | ||
http2ConnectionDebugInitializer: | ||
(@Sendable (Channel) -> EventLoopFuture<Void>)? = nil, | ||
http2StreamChannelDebugInitializer: | ||
(@Sendable (Channel) -> EventLoopFuture<Void>)? = nil | ||
|
||
) { | ||
self.tlsConfiguration = tlsConfiguration | ||
self.redirectConfiguration = redirectConfiguration ?? RedirectConfiguration() | ||
|
@@ -865,6 +883,9 @@ public class HTTPClient { | |
self.httpVersion = .automatic | ||
self.networkFrameworkWaitForConnectivity = true | ||
self.enableMultipath = false | ||
self.http1_1ConnectionDebugInitializer = http1_1ConnectionDebugInitializer | ||
self.http2ConnectionDebugInitializer = http2ConnectionDebugInitializer | ||
self.http2StreamChannelDebugInitializer = http2StreamChannelDebugInitializer | ||
} | ||
|
||
public init( | ||
|
@@ -873,7 +894,13 @@ public class HTTPClient { | |
timeout: Timeout = Timeout(), | ||
proxy: Proxy? = nil, | ||
ignoreUncleanSSLShutdown: Bool = false, | ||
decompression: Decompression = .disabled | ||
decompression: Decompression = .disabled, | ||
http1_1ConnectionDebugInitializer: | ||
(@Sendable (Channel) -> EventLoopFuture<Void>)? = nil, | ||
http2ConnectionDebugInitializer: | ||
(@Sendable (Channel) -> EventLoopFuture<Void>)? = nil, | ||
http2StreamChannelDebugInitializer: | ||
(@Sendable (Channel) -> EventLoopFuture<Void>)? = nil | ||
) { | ||
self.init( | ||
tlsConfiguration: tlsConfiguration, | ||
|
@@ -882,7 +909,10 @@ public class HTTPClient { | |
connectionPool: ConnectionPool(), | ||
proxy: proxy, | ||
ignoreUncleanSSLShutdown: ignoreUncleanSSLShutdown, | ||
decompression: decompression | ||
decompression: decompression, | ||
http1_1ConnectionDebugInitializer: http1_1ConnectionDebugInitializer, | ||
http2ConnectionDebugInitializer: http2ConnectionDebugInitializer, | ||
http2StreamChannelDebugInitializer: http2StreamChannelDebugInitializer | ||
) | ||
} | ||
|
||
|
@@ -893,7 +923,13 @@ public class HTTPClient { | |
maximumAllowedIdleTimeInConnectionPool: TimeAmount = .seconds(60), | ||
proxy: Proxy? = nil, | ||
ignoreUncleanSSLShutdown: Bool = false, | ||
decompression: Decompression = .disabled | ||
decompression: Decompression = .disabled, | ||
http1_1ConnectionDebugInitializer: | ||
(@Sendable (Channel) -> EventLoopFuture<Void>)? = nil, | ||
http2ConnectionDebugInitializer: | ||
(@Sendable (Channel) -> EventLoopFuture<Void>)? = nil, | ||
http2StreamChannelDebugInitializer: | ||
(@Sendable (Channel) -> EventLoopFuture<Void>)? = nil | ||
) { | ||
var tlsConfig = TLSConfiguration.makeClientConfiguration() | ||
tlsConfig.certificateVerification = certificateVerification | ||
|
@@ -904,7 +940,10 @@ public class HTTPClient { | |
connectionPool: ConnectionPool(idleTimeout: maximumAllowedIdleTimeInConnectionPool), | ||
proxy: proxy, | ||
ignoreUncleanSSLShutdown: ignoreUncleanSSLShutdown, | ||
decompression: decompression | ||
decompression: decompression, | ||
http1_1ConnectionDebugInitializer: http1_1ConnectionDebugInitializer, | ||
http2ConnectionDebugInitializer: http2ConnectionDebugInitializer, | ||
http2StreamChannelDebugInitializer: http2StreamChannelDebugInitializer | ||
) | ||
} | ||
|
||
|
@@ -916,7 +955,13 @@ public class HTTPClient { | |
proxy: Proxy? = nil, | ||
ignoreUncleanSSLShutdown: Bool = false, | ||
decompression: Decompression = .disabled, | ||
backgroundActivityLogger: Logger? | ||
backgroundActivityLogger: Logger?, | ||
http1_1ConnectionDebugInitializer: | ||
(@Sendable (Channel) -> EventLoopFuture<Void>)? = nil, | ||
http2ConnectionDebugInitializer: | ||
(@Sendable (Channel) -> EventLoopFuture<Void>)? = nil, | ||
http2StreamChannelDebugInitializer: | ||
(@Sendable (Channel) -> EventLoopFuture<Void>)? = nil | ||
) { | ||
var tlsConfig = TLSConfiguration.makeClientConfiguration() | ||
tlsConfig.certificateVerification = certificateVerification | ||
|
@@ -927,7 +972,10 @@ public class HTTPClient { | |
connectionPool: ConnectionPool(idleTimeout: connectionPool), | ||
proxy: proxy, | ||
ignoreUncleanSSLShutdown: ignoreUncleanSSLShutdown, | ||
decompression: decompression | ||
decompression: decompression, | ||
http1_1ConnectionDebugInitializer: http1_1ConnectionDebugInitializer, | ||
http2ConnectionDebugInitializer: http2ConnectionDebugInitializer, | ||
http2StreamChannelDebugInitializer: http2StreamChannelDebugInitializer | ||
) | ||
} | ||
|
||
|
@@ -937,7 +985,13 @@ public class HTTPClient { | |
timeout: Timeout = Timeout(), | ||
proxy: Proxy? = nil, | ||
ignoreUncleanSSLShutdown: Bool = false, | ||
decompression: Decompression = .disabled | ||
decompression: Decompression = .disabled, | ||
http1_1ConnectionDebugInitializer: | ||
(@Sendable (Channel) -> EventLoopFuture<Void>)? = nil, | ||
http2ConnectionDebugInitializer: | ||
(@Sendable (Channel) -> EventLoopFuture<Void>)? = nil, | ||
http2StreamChannelDebugInitializer: | ||
(@Sendable (Channel) -> EventLoopFuture<Void>)? = nil | ||
) { | ||
self.init( | ||
certificateVerification: certificateVerification, | ||
|
@@ -946,7 +1000,10 @@ public class HTTPClient { | |
maximumAllowedIdleTimeInConnectionPool: .seconds(60), | ||
proxy: proxy, | ||
ignoreUncleanSSLShutdown: ignoreUncleanSSLShutdown, | ||
decompression: decompression | ||
decompression: decompression, | ||
http1_1ConnectionDebugInitializer: http1_1ConnectionDebugInitializer, | ||
http2ConnectionDebugInitializer: http2ConnectionDebugInitializer, | ||
http2StreamChannelDebugInitializer: http2StreamChannelDebugInitializer | ||
) | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4306,4 +4306,89 @@ final class HTTPClientTests: XCTestCaseHTTPClientTestsBaseClass { | |
request.setBasicAuth(username: "foo", password: "bar") | ||
XCTAssertEqual(request.headers.first(name: "Authorization"), "Basic Zm9vOmJhcg==") | ||
} | ||
|
||
func testHTTP1ConnectionDebugInitializer() { | ||
let connectionDebugInitializerUtil = DebugInitializerUtil() | ||
|
||
var config = HTTPClient.Configuration() | ||
config.tlsConfiguration = .clientDefault | ||
config.tlsConfiguration?.certificateVerification = .none | ||
config.httpVersion = .http1Only | ||
config.http1_1ConnectionDebugInitializer = connectionDebugInitializerUtil.operation | ||
|
||
let client = HTTPClient( | ||
eventLoopGroupProvider: .singleton, | ||
configuration: config, | ||
backgroundActivityLogger: Logger( | ||
label: "HTTPClient", | ||
factory: StreamLogHandler.standardOutput(label:) | ||
) | ||
) | ||
defer { XCTAssertNoThrow(client.shutdown()) } | ||
|
||
let bin = HTTPBin(.http1_1(ssl: true, compress: false)) | ||
|
||
defer { XCTAssertNoThrow(try bin.shutdown()) } | ||
|
||
for _ in 0..<3 { | ||
XCTAssertNoThrow(try client.get(url: "https://localhost:\(bin.port)/get").wait()) | ||
} | ||
|
||
// Even though multiple requests were made, the connection debug initializer must be called | ||
// only once. | ||
XCTAssertEqual(connectionDebugInitializerUtil.executionCount, 1) | ||
} | ||
|
||
func testHTTP2ConnectionAndStreamChannelDebugInitializers() { | ||
let connectionDebugInitializerUtil = DebugInitializerUtil() | ||
let streamChannelDebugInitializerUtil = DebugInitializerUtil() | ||
|
||
var config = HTTPClient.Configuration() | ||
config.tlsConfiguration = .clientDefault | ||
config.tlsConfiguration?.certificateVerification = .none | ||
config.httpVersion = .automatic | ||
config.http2ConnectionDebugInitializer = connectionDebugInitializerUtil.operation | ||
config.http2StreamChannelDebugInitializer = streamChannelDebugInitializerUtil.operation | ||
|
||
let client = HTTPClient( | ||
eventLoopGroupProvider: .singleton, | ||
configuration: config, | ||
backgroundActivityLogger: Logger( | ||
label: "HTTPClient", | ||
factory: StreamLogHandler.standardOutput(label:) | ||
) | ||
) | ||
defer { XCTAssertNoThrow(client.shutdown()) } | ||
|
||
let bin = HTTPBin(.http2(compress: false)) | ||
defer { XCTAssertNoThrow(try bin.shutdown()) } | ||
|
||
let numberOfRequests = 3 | ||
|
||
for _ in 0..<numberOfRequests { | ||
XCTAssertNoThrow(try client.get(url: "https://localhost:\(bin.port)/get").wait()) | ||
} | ||
|
||
// Even though multiple requests were made, the connection debug initializer must be called | ||
// only once. | ||
XCTAssertEqual(connectionDebugInitializerUtil.executionCount, 1) | ||
|
||
// The stream channel debug initializer must be called only as much as the number of | ||
// requests made. | ||
XCTAssertEqual(streamChannelDebugInitializerUtil.executionCount, numberOfRequests) | ||
} | ||
} | ||
|
||
class DebugInitializerUtil { | ||
|
||
var executionCount: Int | ||
|
||
@Sendable | ||
glbrntt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
func operation(channel: Channel) -> EventLoopFuture<Void> { | ||
|
||
self.executionCount += 1 | ||
|
||
return channel.eventLoop.makeSucceededVoidFuture() | ||
|
||
} | ||
|
||
init() { | ||
self.executionCount = 0 | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this API should change. Instead I think the initialiser should be passed in to the
start
method. The initialiser isn't something that changes; it remains the same for all requests so it's really a property of the connection.