-
Notifications
You must be signed in to change notification settings - Fork 120
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
Add "debug initializer" hook for channels #801
base: main
Are you sure you want to change the base?
Changes from 1 commit
9e26b5e
f51ae1c
a323796
0d22e92
4847b6e
d47e756
fec1b80
77ffedf
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 | ||
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. Adding parameters to a function is a SemVer breaking change, even if they have defaults. The reason is that you could store the function in a variable: struct Foo {
var bar: Int
init(bar: Int) { ... }
}
// This is totally value
let makeFoo = Foo.init(bar:)
let foo = makeFoo(bar: 42) // equivalent to Foo(bar: 42) If you subsequently add a parameter to the The upshot of this is that you should add a new |
||
) { | ||
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)) | ||
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. Presumably the initialiser also works when we're not using SSL? I'm not super familiar with this codebase so would be good to check that. |
||
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 { | ||
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 isn't a very helpful name, something like |
||
var executionCount: Int | ||
|
||
@Sendable | ||
glbrntt marked this conversation as resolved.
Show resolved
Hide resolved
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. I don't think we need this annotation as the type is 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. A warning is given for the implicit conversion. 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. I think that's because you do http1_1ConnectionDebugInitializer: { channel in
connectionDebugInitializerUtil.initialize(channel: channel)
} |
||
func operation(channel: Channel) -> EventLoopFuture<Void> { | ||
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. in light of the comment above re: naming, |
||
self.executionCount += 1 | ||
|
||
return channel.eventLoop.makeSucceededVoidFuture() | ||
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. It may be useful to add some tests that have the debug initializer actually delay the setup. That is, make sure that we actually do wait for this to complete. Easiest thing to do might be to set some low timeouts, and then confirm the timeouts fire, and that completing the promise doesn't then cause any issues. |
||
} | ||
|
||
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.